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



50
51
52
53
54
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 50

def button(action, label = nil, args = {})
  instance_exec do
    before_action { _insert_button(action, label, args) }
  end
end

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

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



77
78
79
80
81
82
83
84
85
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 77

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



58
59
60
61
62
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 58

def on(action, args = {})
  instance_exec do
    before_action { _insert_on(action, args) }
  end
end

#page_title(label = nil, opts = {}, &block) ⇒ Object

page_title ‘My Title’, only: [:new]



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 65

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 }



98
99
100
101
102
103
104
105
106
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 98

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, 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
40
41
# File 'app/controllers/concerns/effective/crud_controller/dsl.rb', line 37

def submit(action, label, args = {})
  instance_exec do
    before_action { _insert_submit(action, label, args) }
  end
end