Class: AbAdmin::AbstractResource

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ab_admin/abstract_resource.rb

Constant Summary collapse

ACTIONS =
[:index, :show, :new, :edit, :create, :update, :destroy, :preview, :batch, :rebuild, :custom_action, :history]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstractResource

Returns a new instance of AbstractResource



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ab_admin/abstract_resource.rb', line 13

def initialize
  @actions = ACTIONS
  @custom_settings = {}
  @batch_action_list = [AbAdmin::Config::BatchAction.new(:destroy, confirm: I18n.t('admin.delete_confirmation'))]
  @action_items = []
  @disabled_action_items = []
  @default_action_items_for = {}
  @action_items_for = {}
  @parent_resources = []
  @custom_actions = []
  @scopes = []
  @model = self.class.name.sub('AbAdmin', '').safe_constantize
  add_admin_addition_to_model
end

Instance Attribute Details

#action_itemsObject

Returns the value of attribute action_items



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def action_items
  @action_items
end

#actionsObject

Returns the value of attribute actions



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def actions
  @actions
end

#batch_action_listObject

Returns the value of attribute batch_action_list



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def batch_action_list
  @batch_action_list
end

#custom_actionsObject

Returns the value of attribute custom_actions



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def custom_actions
  @custom_actions
end

#custom_settingsObject

Returns the value of attribute custom_settings



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def custom_settings
  @custom_settings
end

#disabled_action_itemsObject

Returns the value of attribute disabled_action_items



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def disabled_action_items
  @disabled_action_items
end

#exportObject

Returns the value of attribute export



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def export
  @export
end

#formObject

Returns the value of attribute form



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def form
  @form
end

Returns the value of attribute modal_form



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def modal_form
  @modal_form
end

#modelObject

Returns the value of attribute model



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def model
  @model
end

#parent_resourcesObject

Returns the value of attribute parent_resources



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def parent_resources
  @parent_resources
end

#permitted_paramsObject

Returns the value of attribute permitted_params



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def permitted_params
  @permitted_params
end

#preview_pathObject

Returns the value of attribute preview_path



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def preview_path
  @preview_path
end

#resource_action_itemsObject

Returns the value of attribute resource_action_items



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def resource_action_items
  @resource_action_items
end

#scopesObject

Returns the value of attribute scopes



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def scopes
  @scopes
end

#searchObject

Returns the value of attribute search



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def search
  @search
end

#showObject

Returns the value of attribute show



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def show
  @show
end

#tableObject

Returns the value of attribute table



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def table
  @table
end

#tree_node_rendererObject

Returns the value of attribute tree_node_renderer



9
10
11
# File 'lib/ab_admin/abstract_resource.rb', line 9

def tree_node_renderer
  @tree_node_renderer
end

Class Method Details

.action_item(*args, &block) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/ab_admin/abstract_resource.rb', line 96

def action_item(*args, &block)
  options = args.extract_options!
  if block_given?
    instance.action_items << AbAdmin::Config::ActionItem.new(options, &block)
  elsif args[1].is_a?(FalseClass)
    instance.disabled_action_items << args[0]
  end
end

.actions(*actions_to_keep) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ab_admin/abstract_resource.rb', line 66

def actions(*actions_to_keep)
  instance.actions = begin
    options = actions_to_keep.extract_options!
    if options[:except]
      ACTIONS - Array(options[:except]).map(&:to_sym)
    else
      actions_to_keep
    end << :custom_action
  end.map(&:to_sym)
end

.batch_action(name, options = {}, &block) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/ab_admin/abstract_resource.rb', line 88

def batch_action(name, options={}, &block)
  if options
    instance.batch_action_list << AbAdmin::Config::BatchAction.new(name.to_sym, options, &block)
  else
    instance.batch_action_list.reject!{|a| a.name == name.to_sym }
  end
end

.belongs_to(*args) ⇒ Object



117
118
119
120
121
122
# File 'lib/ab_admin/abstract_resource.rb', line 117

def belongs_to(*args)
  options = args.extract_options!
  args.each do |name|
    instance.parent_resources << OpenStruct.new(name: name, options: options)
  end
end

.collection_action(name, options = {}, &block) ⇒ Object



128
129
130
# File 'lib/ab_admin/abstract_resource.rb', line 128

def collection_action(name, options={}, &block)
  instance.custom_actions << AbAdmin::Config::CustomAction.new(name, options.merge(collection: true), &block)
end

.export(options = {}, &block) ⇒ Object



42
43
44
# File 'lib/ab_admin/abstract_resource.rb', line 42

def export(options={}, &block)
  instance.export = ::AbAdmin::Config::Export.new(options, &block)
end

.form(options = {}, &block) ⇒ Object



46
47
48
# File 'lib/ab_admin/abstract_resource.rb', line 46

def form(options={}, &block)
  instance.form = ::AbAdmin::Config::Form.new(options, &block)
end

.member_action(name, options = {}, &block) ⇒ Object



124
125
126
# File 'lib/ab_admin/abstract_resource.rb', line 124

def member_action(name, options={}, &block)
  instance.custom_actions << AbAdmin::Config::CustomAction.new(name, options, &block)
end


50
51
52
# File 'lib/ab_admin/abstract_resource.rb', line 50

def modal_form(options={}, &block)
  instance.modal_form = ::AbAdmin::Config::ModalForm.new(options, &block)
end

.permitted_params(*values, &block) ⇒ Object



62
63
64
# File 'lib/ab_admin/abstract_resource.rb', line 62

def permitted_params(*values, &block)
  instance.permitted_params = block_given? ? block : values
end

.preview_path(value = nil, &block) ⇒ Object



58
59
60
# File 'lib/ab_admin/abstract_resource.rb', line 58

def preview_path(value=nil, &block)
  instance.preview_path = block_given? ? block : value
end

.resource_action_item(options = {}, &block) ⇒ Object



109
110
111
# File 'lib/ab_admin/abstract_resource.rb', line 109

def resource_action_item(options={}, &block)
  instance.resource_action_items << AbAdmin::Config::ActionItem.new(options, &block)
end

.resource_action_items(*actions) ⇒ Object



105
106
107
# File 'lib/ab_admin/abstract_resource.rb', line 105

def resource_action_items(*actions)
  instance.resource_action_items = actions + instance.resource_action_items.find_all { |a| a.is_a?(AbAdmin::Config::ActionItem) }
end

.scope(name, options = {}, &block) ⇒ Object



132
133
134
# File 'lib/ab_admin/abstract_resource.rb', line 132

def scope(name, options={}, &block)
  instance.scopes << AbAdmin::Config::Scope.new(name, options, &block)
end

.search(options = {}, &block) ⇒ Object



38
39
40
# File 'lib/ab_admin/abstract_resource.rb', line 38

def search(options={}, &block)
  instance.search = AbAdmin::Config::Search.new(options, &block)
end

.settings(value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/ab_admin/abstract_resource.rb', line 77

def settings(value)
  instance.custom_settings = value
  if instance.custom_settings[:history]
    instance.custom_settings[:history] = {} unless instance.custom_settings[:history].is_a?(Hash)
    instance.model.send(:include, AbAdmin::Concerns::HasTracking) unless instance.has_module?(AbAdmin::Concerns::HasTracking)
  end
  if instance.custom_settings[:comments]
    instance.custom_settings[:comments] = {} unless instance.custom_settings[:comments].is_a?(Hash)
  end
end

.show(options = {}, &block) ⇒ Object



54
55
56
# File 'lib/ab_admin/abstract_resource.rb', line 54

def show(options={}, &block)
  instance.show = ::AbAdmin::Config::Show.new(options, &block)
end

.table(options = {}, &block) ⇒ Object



34
35
36
# File 'lib/ab_admin/abstract_resource.rb', line 34

def table(options={}, &block)
  instance.table = AbAdmin::Config::Table.new(options, &block)
end

.tree(&block) ⇒ Object



113
114
115
# File 'lib/ab_admin/abstract_resource.rb', line 113

def tree(&block)
  instance.tree_node_renderer = block
end

Instance Method Details

#action_items_for(action) ⇒ Object



146
147
148
# File 'lib/ab_admin/abstract_resource.rb', line 146

def action_items_for(action)
  @action_items_for[action] ||= action_items.find_all{|i| i.for_action?(action) }
end

#add_admin_addition_to_modelObject



28
29
30
31
# File 'lib/ab_admin/abstract_resource.rb', line 28

def add_admin_addition_to_model
  return unless @model
  @model.send(:include, AbAdmin::Concerns::AdminAddition) unless has_module?(AbAdmin::Concerns::AdminAddition)
end

#allow_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/ab_admin/abstract_resource.rb', line 141

def allow_action?(action)
  return true unless actions
  actions.include?(action.to_sym)
end

#custom_action_for(name, context) ⇒ Object



166
167
168
169
170
# File 'lib/ab_admin/abstract_resource.rb', line 166

def custom_action_for(name, context)
  custom_action = @custom_actions.detect { |a| a.name == name.to_sym }
  raise "No allowed custom action found #{name}" if !custom_action || !custom_action.for_context?(context)
  custom_action
end

#default_action_items_for(action, for_resource) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ab_admin/abstract_resource.rb', line 150

def default_action_items_for(action, for_resource)
  @default_action_items_for[action] ||= begin
    base = [:new]
    if for_resource
      base += [:edit, :show, :destroy, :preview]
      base << :history if custom_settings[:history] && !custom_settings[:history][:sidebar]
    end
    disabled = action == :new ? [] : [action]
    (base - disabled - @disabled_action_items) & @actions
  end
end

#has_module?(module_constant) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/ab_admin/abstract_resource.rb', line 172

def has_module?(module_constant)
  model.included_modules.include?(module_constant)
end