Module: Effective::CrudController::ClassMethods
- Defined in:
- app/controllers/concerns/effective/crud_controller.rb
Instance Method Summary collapse
- #after_error(*names, &blk) ⇒ Object
- #after_save(*names, &blk) ⇒ Object
- #before_render(*names, &blk) ⇒ Object
- #collection_action(action) ⇒ Object
-
#default_actions(args = {}) ⇒ Object
Applies the default actions.
-
#member_action(action, commit = nil, args = {}) ⇒ Object
Add the following to your controller for a simple member action member_action :print.
-
#page_title(label = nil, opts = {}, &block) ⇒ Object
page_title ‘My Title’, only: [:new].
-
#resource_scope(obj = nil, opts = {}, &block) ⇒ Object
Return value should be: a Relation: Thing.where(user: current_user) a Hash: { user_id: current_user.id }.
Instance Method Details
#after_error(*names, &blk) ⇒ Object
34 35 36 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 34 def after_error(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_error, :after, name, ) } end |
#after_save(*names, &blk) ⇒ Object
30 31 32 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 30 def after_save(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_save, :after, name, ) } end |
#before_render(*names, &blk) ⇒ Object
26 27 28 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 26 def before_render(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_render, :before, name, ) } end |
#collection_action(action) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 82 def collection_action(action) define_method(action) do if params[:ids].present? self.resources ||= resource_scope.where(id: params[:ids]) end if effective_resource.scope?(action) self.resources ||= resource_scope.public_send(action) end self.resources ||= resource_scope.all EffectiveResources.(self, action, resource_klass) @page_title ||= "#{action.to_s.titleize} #{resource_plural_name.titleize}" collection_post_action(action) unless request.get? end end |
#default_actions(args = {}) ⇒ Object
Applies the default actions
103 104 105 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 103 def default_actions(args = {}) default_member_actions.each { |k, v| member_actions[k] = (args || {}).merge(v) } end |
#member_action(action, commit = nil, args = {}) ⇒ Object
Add the following to your controller for a simple member action member_action :print
Add to your permissions: can :print, Thing
If you want to POST and do an action based on this: Make sure your model responds to approve! member_action :approve If you want it to automatically show up in your forms member_action :approve, ‘Save and Approve’, if: -> { approved? } member_action :approve, ‘Save and Approve’, unless: -> { !approved? }, redirect: :show
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 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 50 def member_action(action, commit = nil, args = {}) if commit.present? raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false if args == false member_actions.delete(commit); return end if args.key?(:if) && args[:if].respond_to?(:call) == false raise "expected if: to be callable. Try member_action :approve, 'Save and Approve', if: -> { submitted? }" end if args.key?(:unless) && args[:unless].respond_to?(:call) == false raise "expected unless: to be callable. Try member_action :approve, 'Save and Approve', unless: -> { declined? }" end redirect = args.delete(:redirect_to) || args.delete(:redirect) # Remove redirect_to keyword. use redirect. member_actions[commit] = (args || {}).merge(action: action, redirect: redirect) end define_method(action) do self.resource ||= resource_scope.find(params[:id]) EffectiveResources.(self, action, resource) @page_title ||= "#{action.to_s.titleize} #{resource}" member_post_action(action) unless request.get? end end |
#page_title(label = nil, opts = {}, &block) ⇒ Object
page_title ‘My Title’, only: [:new]
108 109 110 111 112 113 114 115 116 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 108 def page_title(label = nil, opts = {}, &block) raise 'expected a label or block' unless (label || block_given?) instance_exec do before_action(opts) do @page_title ||= (block_given? ? instance_exec(&block) : label) end end end |
#resource_scope(obj = nil, opts = {}, &block) ⇒ Object
Return value should be: a Relation: Thing.where(user: current_user) a Hash: { user_id: current_user.id }
127 128 129 130 131 132 133 134 135 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 127 def resource_scope(obj = nil, opts = {}, &block) raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?) instance_exec do before_action(opts) do @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj)) end end end |