Module: Pakyow::Reflection::Extension::Controller
- Extended by:
- Support::Extension
- Defined in:
- lib/pakyow/reflection/extensions/controller.rb
Instance Method Summary collapse
- #perform_reflected_action ⇒ Object
- #redirect_to_reflected_destination ⇒ Object
- #reflected_destination ⇒ Object
- #reflective_expose ⇒ Object
- #verify_reflected_form ⇒ Object
- #with_reflected_action ⇒ Object
- #with_reflected_endpoint ⇒ Object
Instance Method Details
#perform_reflected_action ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 122 def perform_reflected_action with_reflected_action do |reflected_action| logger.debug { "[reflection] performing `#{[self.class.name_of_self, connection.values[:__endpoint_name]].join("_")}' for `#{reflected_action.view_path}'" } proxy = data.public_send(reflected_action.scope.plural_name) # Pull initial values from the params. # values = params[reflected_action.scope.name] # Associate the object with its parent when creating. # if self.class.parent && connection.get(:__endpoint_name) == :create # TODO: Handle cases where objects are associated by id but routed by another field. # Implement when we allow foreign keys to be specified in associations. # if proxy.source.class.attributes.key?(self.class.parent.nested_param) values[self.class.parent.nested_param] = params[self.class.parent.nested_param] end end # Limit the action for update, delete. # if connection.get(:__endpoint_name) == :update || connection.get(:__endpoint_name) == :delete proxy = proxy.public_send(:"by_#{proxy.source.class.primary_key_field}", params[self.class.param]) trigger 404 if proxy.count == 0 end proxy.transaction do unless connection.get(:__endpoint_name) == :delete handle_nested_values_for_source(values, proxy.source.class) end @object = proxy.send( connection.get(:__endpoint_name), values ) end logger.debug { "[reflection] changes have been saved to the `#{proxy.source.class.plural_name}' data source" } end rescue Data::ConstraintViolation trigger 404 end |
#redirect_to_reflected_destination ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 170 def redirect_to_reflected_destination if destination = reflected_destination logger.debug { "[reflection] redirecting to `#{destination}'" } redirect destination end end |
#reflected_destination ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 180 def reflected_destination with_reflected_action do |reflected_action| if connection.form && origin = connection.form[:origin] if instance_variable_defined?(:@object) if route = self.class.routes[:get].find { |route| route.name == :show } route.build_path(self.class.path_to_self, **params.merge(@object.one.to_h)) elsif route = self.class.routes[:get].find { |route| route.name == :list } route.build_path(self.class.path_to_self, **params) else origin end else origin end else nil end end end |
#reflective_expose ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 31 def reflective_expose reflected_endpoint.exposures.each do |reflected_exposure| if reflected_exposure.parent.nil? || reflected_exposure.binding.to_s.include?("form") if dataset = reflected_exposure.dataset query = data.send(reflected_exposure.scope.plural_name) if dataset.include?(:limit) query = query.limit(dataset[:limit].to_i) end if dataset.include?(:order) query = query.order(*dataset[:order].to_a) end if dataset.include?(:query) && dataset[:query] != "all" case dataset[:query] when Array dataset[:query].each do |key, value| value = if respond_to?(value) public_send(value) else value end query = query.public_send(key, value) end else query = query.public_send(dataset[:query].to_s) end end else query = data.send(reflected_exposure.scope.plural_name) if reflects_specific_object?(reflected_exposure.scope.plural_name) if resource = resource_with_name(reflected_exposure.scope.plural_name) if resource == self.class query_param = resource.param params_param = resource.param else query_param = resource.param params_param = resource.nested_param end query = query.send(:"by_#{query_param}", params[params_param]) end end end query = apply_includes_to_query(query, reflected_exposure.scope.plural_name, reflected_exposure.children) if reflects_specific_object?(reflected_exposure.scope.plural_name) && query.count == 0 trigger 404 else if !reflected_exposure.binding.to_s.include?("form") || reflected_endpoint.view_path.end_with?("/edit") logger.debug { "[reflection] exposing dataset for `#{reflected_exposure.binding}': #{query.inspect}" } expose reflected_exposure.binding.to_s, query end end end end end |
#verify_reflected_form ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 96 def verify_reflected_form with_reflected_action do |reflected_action| local = self verify do required reflected_action.scope.name do reflected_action.attributes.each do |attribute| if attribute.required? required attribute.name do validate :presence end else optional attribute.name end end local.__send__(:verify_nested_data, reflected_action.nested, self) end end logger.debug { "[reflection] verified and validated submitted values for `#{reflected_action.scope.name}'" } end end |
#with_reflected_action ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 15 def with_reflected_action if reflected_action yield reflected_action else trigger 404 end end |
#with_reflected_endpoint ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/pakyow/reflection/extensions/controller.rb', line 23 def with_reflected_endpoint if reflected_endpoint yield reflected_endpoint else trigger 404 end end |