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
Defines a function to handle a GET and POST request on this URL Handles bulk_ actions Just add a member action to your routes, you shouldn’t need to call this directly You shouldn’t need to call this directly.
-
#define_actions_from_routes ⇒ Object
Automatically respond to any action defined via the routes file.
-
#member_action(action) ⇒ Object
Defines a function to handle a GET and POST request on this URL Just add a member action to your routes, you shouldn’t need to call this directly.
-
#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 }.
-
#submit(action, commit = nil, args = {}) ⇒ Object
This controls the form submit options of effective_submit It also controls the redirect path for any actions.
Instance Method Details
#after_error(*names, &blk) ⇒ Object
39 40 41 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 39 def after_error(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_error, :after, name, ) } end |
#after_save(*names, &blk) ⇒ Object
35 36 37 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 35 def after_save(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_save, :after, name, ) } end |
#before_render(*names, &blk) ⇒ Object
31 32 33 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 31 def before_render(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_render, :before, name, ) } end |
#collection_action(action) ⇒ Object
Defines a function to handle a GET and POST request on this URL Handles bulk_ actions Just add a member action to your routes, you shouldn’t need to call this directly You shouldn’t need to call this directly
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 131 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 |
#define_actions_from_routes ⇒ Object
Automatically respond to any action defined via the routes file
24 25 26 27 28 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 24 def define_actions_from_routes resource = Effective::Resource.new(controller_path) resource.member_actions.each { |action| member_action(action) } resource.collection_actions.each { |action| collection_action(action) } end |
#member_action(action) ⇒ Object
Defines a function to handle a GET and POST request on this URL Just add a member action to your routes, you shouldn’t need to call this directly
115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 115 def member_action(action) 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]
84 85 86 87 88 89 90 91 92 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 84 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 }
103 104 105 106 107 108 109 110 111 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 103 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 |
#submit(action, commit = nil, args = {}) ⇒ Object
This controls the form submit options of effective_submit It also controls the redirect path for any actions
Effective::Resource will populate this with all member_post_actions And you can control the details with this DSL:
submit :approve, ‘Save and Approve’, unless: -> { approved? }, redirect: :show
submit :toggle, ‘Blacklist’, if: -> { sync? }, class: ‘btn btn-primary’ submit :toggle, ‘Whitelist’, if: -> { !sync? }, class: ‘btn btn-primary’
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 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 54 def submit(action, commit = nil, args = {}) raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false if commit == false submits.delete_if { |commit, args| args[:action] == action }; return end if args == false submits.delete(commit); return end if commit # Overwrite the default member action when given a custom commit submits.delete_if { |commit, args| args[:default] && args[:action] == action } end if args.key?(:if) && args[:if].respond_to?(:call) == false raise "expected if: to be callable. Try submit :approve, 'Save and Approve', if: -> { finished? }" end if args.key?(:unless) && args[:unless].respond_to?(:call) == false raise "expected unless: to be callable. Try submit :approve, 'Save and Approve', unless: -> { declined? }" end redirect = args.delete(:redirect_to) || args.delete(:redirect) # Remove redirect_to keyword. use redirect. args.merge!(action: action, redirect: redirect) (submits[commit] ||= {}).merge!(args) end |