Module: Rezource
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/rezource.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #action_resource_path(action, *args) ⇒ Object
- #after_create(resource) ⇒ Object
- #after_destroy(resource) ⇒ Object
- #after_update(resource) ⇒ Object
- #before_create(resource) ⇒ Object
- #before_destroy(resource) ⇒ Object
- #before_update(resource) ⇒ Object
- #build_resource(attributes = {}) ⇒ Object
- #build_resource_with_parent(attributes) ⇒ Object
- #build_resource_without_parent(attributes) ⇒ Object
- #create ⇒ Object
- #create_respond_with(resource) ⇒ Object
- #default_respond_with(*args) ⇒ Object
- #destroy ⇒ Object
-
#destroy_respond_with(resource) ⇒ Object
def update_respond_with(resource) respond_with *namespace, resource end.
- #edit ⇒ Object
- #edit_resource_path(*args) ⇒ Object
- #edit_respond_with(resource) ⇒ Object
- #find_parent ⇒ Object
- #find_parent_scope ⇒ Object
- #find_resource ⇒ Object
- #find_resource_scope ⇒ Object
- #find_resources ⇒ Object
- #find_resources_scope ⇒ Object
-
#index ⇒ Object
———-.
-
#index_respond_with(resources) ⇒ Object
———–.
- #interpolation_options ⇒ Object
-
#namespace ⇒ Object
———–.
- #new ⇒ Object
-
#new_resource_path(*args) ⇒ Object
———–.
- #new_respond_with(resource) ⇒ Object
- #parent ⇒ Object
-
#parent? ⇒ Boolean
———–.
- #parent_class ⇒ Object
- #parent_ivar ⇒ Object
- #parent_ivar_get ⇒ Object
- #parent_ivar_set(value) ⇒ Object
- #permitted_params ⇒ Object
- #permitted_params_create ⇒ Object
- #permitted_params_update ⇒ Object
- #resource ⇒ Object
-
#resource_class ⇒ Object
———–.
- #resource_create(resource) ⇒ Object
- #resource_destroy(resource) ⇒ Object
- #resource_ivar ⇒ Object
- #resource_ivar_get ⇒ Object
- #resource_ivar_set(value) ⇒ Object
- #resource_options ⇒ Object
- #resource_path(*args) ⇒ Object
- #resource_update(resource, attributes = {}) ⇒ Object
- #resources ⇒ Object
- #resources_ivar ⇒ Object
- #resources_ivar_get ⇒ Object
- #resources_ivar_set(value) ⇒ Object
- #resources_path(*args) ⇒ Object
- #reverse_set_parent_from_resource(resource) ⇒ Object
- #show ⇒ Object
- #show_respond_with(resource) ⇒ Object
- #update ⇒ Object
Class Method Details
.included(clazz) ⇒ Object
6 7 8 9 |
# File 'lib/rezource.rb', line 6 def self.included(clazz) clazz.helper_method :resource, :resources, :parent clazz.helper_method :resources_path, :resource_path, :new_resource_path, :edit_resource_path end |
Instance Method Details
#action_resource_path(action, *args) ⇒ Object
98 99 100 101 102 |
# File 'lib/rezource.rb', line 98 def action_resource_path(action, *args) parts = [action] + namespace + [resource_ivar] + ["path"] send parts.join("_").to_sym, *args # send raw_path(action).to_sym, *args end |
#after_create(resource) ⇒ Object
242 |
# File 'lib/rezource.rb', line 242 def after_create(resource); end |
#after_destroy(resource) ⇒ Object
244 |
# File 'lib/rezource.rb', line 244 def after_destroy(resource); end |
#after_update(resource) ⇒ Object
243 |
# File 'lib/rezource.rb', line 243 def after_update(resource); end |
#before_create(resource) ⇒ Object
238 |
# File 'lib/rezource.rb', line 238 def before_create(resource); end |
#before_destroy(resource) ⇒ Object
240 |
# File 'lib/rezource.rb', line 240 def before_destroy(resource); end |
#before_update(resource) ⇒ Object
239 |
# File 'lib/rezource.rb', line 239 def before_update(resource); end |
#build_resource(attributes = {}) ⇒ Object
187 188 189 |
# File 'lib/rezource.rb', line 187 def build_resource(attributes={}) resource_ivar_get || resource_ivar_set(send(parent? ? :build_resource_with_parent : :build_resource_without_parent, attributes)) end |
#build_resource_with_parent(attributes) ⇒ Object
191 192 193 |
# File 'lib/rezource.rb', line 191 def build_resource_with_parent(attributes) find_parent.send(resources_ivar.to_sym).build(attributes) end |
#build_resource_without_parent(attributes) ⇒ Object
195 196 197 |
# File 'lib/rezource.rb', line 195 def build_resource_without_parent(attributes) resource_class.new(attributes) end |
#create ⇒ Object
35 36 37 |
# File 'lib/rezource.rb', line 35 def create resource_create build_resource(permitted_params_create) end |
#create_respond_with(resource) ⇒ Object
66 67 68 |
# File 'lib/rezource.rb', line 66 def create_respond_with(resource) respond_with *namespace, resource end |
#default_respond_with(*args) ⇒ Object
148 149 150 |
# File 'lib/rezource.rb', line 148 def default_respond_with(*args) respond_with *([*namespace, resource] + args) end |
#destroy ⇒ Object
48 49 50 |
# File 'lib/rezource.rb', line 48 def destroy resource_destroy find_resource end |
#destroy_respond_with(resource) ⇒ Object
def update_respond_with(resource)
respond_with *namespace, resource
end
78 79 80 |
# File 'lib/rezource.rb', line 78 def destroy_respond_with(resource) respond_with *namespace, resource end |
#edit ⇒ Object
39 40 41 42 |
# File 'lib/rezource.rb', line 39 def edit # respond_with *namespace, find_resource edit_respond_with find_resource end |
#edit_resource_path(*args) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/rezource.rb', line 91 def edit_resource_path(*args) action_resource_path "edit", *args # parts = ["edit"] + namespace + [resource_ivar] + ["path"] # send parts.join("_").to_sym, *args # send raw_path("edit").to_sym, *args end |
#edit_respond_with(resource) ⇒ Object
70 71 72 |
# File 'lib/rezource.rb', line 70 def edit_respond_with(resource) respond_with *namespace, resource end |
#find_parent ⇒ Object
167 168 169 |
# File 'lib/rezource.rb', line 167 def find_parent parent_ivar_get || parent_ivar_set(find_parent_scope.first) end |
#find_parent_scope ⇒ Object
171 172 173 |
# File 'lib/rezource.rb', line 171 def find_parent_scope parent_class.where(id: params[:"#{parent_ivar}_id"]) end |
#find_resource ⇒ Object
207 208 209 210 211 212 213 214 |
# File 'lib/rezource.rb', line 207 def find_resource return resource_ivar_get if resource_ivar_get resource = find_resource_scope.first reverse_set_parent_from_resource resource if parent? resource_ivar_set resource end |
#find_resource_scope ⇒ Object
216 217 218 |
# File 'lib/rezource.rb', line 216 def find_resource_scope resource_class.where(id: params[:id]) end |
#find_resources ⇒ Object
199 200 201 |
# File 'lib/rezource.rb', line 199 def find_resources resources_ivar_get || resources_ivar_set(find_resources_scope.to_a) end |
#find_resources_scope ⇒ Object
203 204 205 |
# File 'lib/rezource.rb', line 203 def find_resources_scope parent? ? find_parent.send(resources_ivar.to_sym) : resource_class end |
#index ⇒ Object
20 21 22 23 |
# File 'lib/rezource.rb', line 20 def index # respond_with *namespace, find_resources index_respond_with find_resources end |
#index_respond_with(resources) ⇒ Object
54 55 56 |
# File 'lib/rezource.rb', line 54 def index_respond_with(resources) respond_with *namespace, resources end |
#interpolation_options ⇒ Object
287 288 289 |
# File 'lib/rezource.rb', line 287 def {resource_name: resource.respond_to?(:name) ? resource.send(:name) : "NAME"} end |
#namespace ⇒ Object
144 145 146 |
# File 'lib/rezource.rb', line 144 def namespace self.class.name.split("::").slice(0...-1).map(&:underscore).map(&:to_sym) end |
#new ⇒ Object
30 31 32 33 |
# File 'lib/rezource.rb', line 30 def new # respond_with *namespace, build_resource new_respond_with build_resource end |
#new_resource_path(*args) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/rezource.rb', line 84 def new_resource_path(*args) parts = ["new"] + namespace + (parent? ? [parent_ivar] : []) + [resource_ivar] + ["path"] send parts.join("_").to_sym, *args # send raw_path("new").to_sym, *args end |
#new_respond_with(resource) ⇒ Object
62 63 64 |
# File 'lib/rezource.rb', line 62 def new_respond_with(resource) respond_with *namespace, resource end |
#parent ⇒ Object
178 |
# File 'lib/rezource.rb', line 178 def parent; parent_ivar_get; end |
#parent? ⇒ Boolean
158 159 160 |
# File 'lib/rezource.rb', line 158 def parent? parent_class end |
#parent_class ⇒ Object
162 163 164 165 |
# File 'lib/rezource.rb', line 162 def parent_class belongs_to = [:belongs_to] belongs_to ? (belongs_to.is_a?(Class) ? belongs_to : belongs_to.to_s.camelcase.constantize) : nil end |
#parent_ivar ⇒ Object
175 |
# File 'lib/rezource.rb', line 175 def parent_ivar; parent_class.name.split("::").last.underscore; end |
#parent_ivar_get ⇒ Object
177 |
# File 'lib/rezource.rb', line 177 def parent_ivar_get; instance_variable_get "@#{parent_ivar}"; end |
#parent_ivar_set(value) ⇒ Object
176 |
# File 'lib/rezource.rb', line 176 def parent_ivar_set(value); instance_variable_set("@#{parent_ivar}", value); value; end |
#permitted_params ⇒ Object
284 |
# File 'lib/rezource.rb', line 284 def permitted_params; raise RuntimeError.new("implement me"); end |
#permitted_params_create ⇒ Object
282 |
# File 'lib/rezource.rb', line 282 def permitted_params_create; permitted_params; end |
#permitted_params_update ⇒ Object
283 |
# File 'lib/rezource.rb', line 283 def permitted_params_update; permitted_params; end |
#resource ⇒ Object
231 |
# File 'lib/rezource.rb', line 231 def resource; resource_ivar_get || find_resource; end |
#resource_class ⇒ Object
183 184 185 |
# File 'lib/rezource.rb', line 183 def resource_class self.class.name.split("::").last.gsub(/Controller$/, "").singularize.constantize end |
#resource_create(resource) ⇒ Object
246 247 248 249 250 251 252 253 |
# File 'lib/rezource.rb', line 246 def resource_create(resource) before_create resource resource.save if resource.valid? after_create resource # respond_with *namespace, resource create_respond_with resource end |
#resource_destroy(resource) ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/rezource.rb', line 272 def resource_destroy(resource) before_destroy resource resource.destroy after_destroy resource # respond_with *namespace, resource destroy_respond_with resource end |
#resource_ivar ⇒ Object
228 |
# File 'lib/rezource.rb', line 228 def resource_ivar; resource_class.name.split("::").last.underscore; end |
#resource_ivar_get ⇒ Object
230 |
# File 'lib/rezource.rb', line 230 def resource_ivar_get; instance_variable_get "@#{resource_ivar}"; end |
#resource_ivar_set(value) ⇒ Object
229 |
# File 'lib/rezource.rb', line 229 def resource_ivar_set(value); instance_variable_set("@#{resource_ivar}", value); value; end |
#resource_options ⇒ Object
152 153 154 |
# File 'lib/rezource.rb', line 152 def ||= (self.class.instance_variable_get("@rezource") || {}) end |
#resource_path(*args) ⇒ Object
110 111 112 113 114 |
# File 'lib/rezource.rb', line 110 def resource_path(*args) parts = namespace + [resource_ivar] + ["path"] send parts.join("_").to_sym, *args # send raw_path(nil, true).to_sym, *args end |
#resource_update(resource, attributes = {}) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/rezource.rb', line 255 def resource_update(resource, attributes={}) before_update resource resource.update_attributes attributes if resource.valid? after_update resource if _action = params[:_action] if resource.valid? respond_with *namespace, resource, location: action_resource_path(_action) else respond_with *namespace, resource, action: _action end else respond_with *namespace, resource end end |
#resources ⇒ Object
236 |
# File 'lib/rezource.rb', line 236 def resources; resources_ivar_get; end |
#resources_ivar ⇒ Object
233 |
# File 'lib/rezource.rb', line 233 def resources_ivar; resource_class.name.split("::").last.pluralize.underscore; end |
#resources_ivar_get ⇒ Object
235 |
# File 'lib/rezource.rb', line 235 def resources_ivar_get; instance_variable_get "@#{resources_ivar}"; end |
#resources_ivar_set(value) ⇒ Object
234 |
# File 'lib/rezource.rb', line 234 def resources_ivar_set(value); instance_variable_set("@#{resources_ivar}", value); value; end |
#resources_path(*args) ⇒ Object
104 105 106 107 108 |
# File 'lib/rezource.rb', line 104 def resources_path(*args) parts = namespace + (parent? ? [parent_ivar] : []) + [[:singleton] ? resource_ivar : resources_ivar] + ["path"] send parts.join("_").to_sym, *args # send raw_path.to_sym, *args end |
#reverse_set_parent_from_resource(resource) ⇒ Object
220 221 222 |
# File 'lib/rezource.rb', line 220 def reverse_set_parent_from_resource(resource) parent_ivar_set resource.send(parent_ivar.to_sym) if resource.respond_to?(parent_ivar.to_sym) end |
#show ⇒ Object
25 26 27 28 |
# File 'lib/rezource.rb', line 25 def show # respond_with *namespace, find_resource show_respond_with find_resource end |
#show_respond_with(resource) ⇒ Object
58 59 60 |
# File 'lib/rezource.rb', line 58 def show_respond_with(resource) respond_with *namespace, resource end |
#update ⇒ Object
44 45 46 |
# File 'lib/rezource.rb', line 44 def update resource_update find_resource, permitted_params_update end |