Module: Effective::CrudController::Dsl
- Included in:
- ClassMethods
- Defined in:
- app/controllers/concerns/effective/crud_controller/dsl.rb
Instance Method Summary collapse
- #after_commit(*names, &blk) ⇒ Object
- #after_error(*names, &blk) ⇒ Object
- #after_save(*names, &blk) ⇒ Object
- #before_render(*names, &blk) ⇒ Object
- #before_save(*names, &blk) ⇒ Object
-
#button(action, label = nil, args = {}) ⇒ Object
This controls the resource buttons section of the page Takes precidence over any on commands.
-
#on(action, args = {}) ⇒ Object
This is a way of defining the redirect, flash etc of any action without tweaking defaults submit and buttons options will be merged ontop of these.
-
#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, label = nil, args = {}) ⇒ Object
This controls the form submit options of effective_submit Takes precidence over any ‘on’ dsl commands.
Instance Method Details
#after_commit(*names, &blk) ⇒ Object
18 19 20 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 18 def after_commit(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_after_commit, :after, name, ) } end |
#after_error(*names, &blk) ⇒ Object
22 23 24 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 22 def after_error(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_error, :after, name, ) } end |
#after_save(*names, &blk) ⇒ Object
14 15 16 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 14 def after_save(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_after_save, :after, name, ) } end |
#before_render(*names, &blk) ⇒ Object
6 7 8 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 6 def before_render(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_render, :before, name, ) } end |
#before_save(*names, &blk) ⇒ Object
10 11 12 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 10 def before_save(*names, &blk) _insert_callbacks(names, blk) { |name, | set_callback(:resource_before_save, :after, name, ) } end |
#button(action, label = nil, args = {}) ⇒ Object
This controls the resource buttons section of the page Takes precidence over any on commands
Effective::Resource will populate this with all member_actions
button :approve, ‘Approve’, unless: -> { resource.approved? }, redirect: :show button :decline, false
48 49 50 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 48 def (action, label = nil, args = {}) (action, label, args) end |
#on(action, args = {}) ⇒ Object
This is a way of defining the redirect, flash etc of any action without tweaking defaults submit and buttons options will be merged ontop of these
54 55 56 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 54 def on(action, args = {}) _insert_on(action, args) end |
#page_title(label = nil, opts = {}, &block) ⇒ Object
page_title ‘My Title’, only: [:new]
59 60 61 62 63 64 65 66 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 59 def page_title(label = nil, opts = {}, &block) opts = label if label.kind_of?(Hash) raise 'expected a label or block' unless (label || block_given?) instance_exec do before_action(opts) { @page_title ||= (block_given? ? instance_exec(&block) : label).to_s } 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 }
79 80 81 82 83 84 85 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 79 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) { @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj)) } end end |
#submit(action, label = nil, args = {}) ⇒ Object
This controls the form submit options of effective_submit Takes precidence over any ‘on’ dsl commands
Effective::Resource will populate this with all crud actions And you can control the details with this DSL:
submit :approve, ‘Save and Approve’, unless: -> { resource.approved? }, redirect: :show
submit :toggle, ‘Blacklist’, if: -> { sync? }, class: ‘btn btn-primary’ submit :toggle, ‘Whitelist’, if: -> { !sync? }, class: ‘btn btn-primary’ submit :save, ‘Save’, success: -> { “#Effective::CrudController#resource was saved okay!” }
37 38 39 |
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 37 def submit(action, label = nil, args = {}) _insert_submit(action, label, args) end |