Module: Effective::CrudController::Dsl

Included in:
ClassMethods
Defined in:
app/controllers/concerns/effective/crud_controller/dsl.rb

Instance Method Summary collapse

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, options| set_callback(:resource_after_commit, :after, name, options) }
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, options| set_callback(:resource_error, :after, name, options) }
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, options| set_callback(:resource_after_save, :after, name, options) }
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, options| set_callback(:resource_render, :before, name, options) }
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, options| set_callback(:resource_before_save, :after, name, options) }
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 button(action, label = nil, args = {})
  _insert_button(action, label, args)
end

#datatable(obj = nil, opts = {}, &block) ⇒ Object

datatable -> { MyDatatable.new }, only: [:index]



71
72
73
74
75
76
77
78
79
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 71

def datatable(obj = nil, opts = {}, &block)
  raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)

  instance_exec do
    before_action(opts) do
      @datatable ||= (block_given? ? instance_exec(&block) : obj.call)
    end
  end
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
67
68
# 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) do
      @page_title ||= (block_given? ? instance_exec(&block) : label).to_s
    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 }



92
93
94
95
96
97
98
99
100
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 92

def resource_scope(obj = nil, opts = {}, &block)
  raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)

  if block_given?
    define_method(:resource_scope_relation) { return block }
  else
    define_method(:resource_scope_relation) { return 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