Class: AbAdmin::AbstractResource
- 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
-
#action_items ⇒ Object
Returns the value of attribute action_items.
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#batch_actions ⇒ Object
Returns the value of attribute batch_actions.
-
#chart ⇒ Object
Returns the value of attribute chart.
-
#custom_actions ⇒ Object
Returns the value of attribute custom_actions.
-
#custom_settings ⇒ Object
Returns the value of attribute custom_settings.
-
#disabled_action_items ⇒ Object
Returns the value of attribute disabled_action_items.
-
#export ⇒ Object
Returns the value of attribute export.
-
#form ⇒ Object
Returns the value of attribute form.
-
#map ⇒ Object
Returns the value of attribute map.
-
#modal_form ⇒ Object
Returns the value of attribute modal_form.
-
#model ⇒ Object
Returns the value of attribute model.
-
#parent_resources ⇒ Object
Returns the value of attribute parent_resources.
-
#permitted_params ⇒ Object
Returns the value of attribute permitted_params.
-
#preview_path ⇒ Object
Returns the value of attribute preview_path.
-
#resource_action_items ⇒ Object
Returns the value of attribute resource_action_items.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
-
#search ⇒ Object
Returns the value of attribute search.
-
#show ⇒ Object
Returns the value of attribute show.
-
#stats ⇒ Object
Returns the value of attribute stats.
-
#table ⇒ Object
Returns the value of attribute table.
-
#tree_node_renderer ⇒ Object
Returns the value of attribute tree_node_renderer.
Class Method Summary collapse
- .action_item(*args, &block) ⇒ Object
- .actions(*actions_to_keep) ⇒ Object
- .batch_action(name, options = {}, &block) ⇒ Object
- .belongs_to(*args) ⇒ Object
- .collection_action(name, options = {}, &block) ⇒ Object
- .export(options = {}, &block) ⇒ Object
- .form(options = {}, &block) ⇒ Object
- .member_action(name, options = {}, &block) ⇒ Object
- .modal_form(options = {}, &block) ⇒ Object
- .permitted_params(*values, &block) ⇒ Object
- .preview_path(value = nil, options = {}, &block) ⇒ Object
- .resource_action_item(options = {}, &block) ⇒ Object
- .resource_action_items(*actions) ⇒ Object
- .scope(name, options = {}, &block) ⇒ Object
- .search(options = {}, &block) ⇒ Object
- .settings(value) ⇒ Object
- .show(options = {}, &block) ⇒ Object
- .table(options = {}, &block) ⇒ Object
- .tree(&block) ⇒ Object
Instance Method Summary collapse
- #action_items_for(action) ⇒ Object
- #add_admin_addition_to_model ⇒ Object
- #allow_action?(action) ⇒ Boolean
- #custom_action_for(name, context) ⇒ Object
- #default_action_items_for(action, for_resource) ⇒ Object
- #has_module?(module_constant) ⇒ Boolean
-
#initialize ⇒ AbstractResource
constructor
A new instance of AbstractResource.
Constructor Details
#initialize ⇒ AbstractResource
Returns a new instance of AbstractResource.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ab_admin/abstract_resource.rb', line 14 def initialize @actions = ACTIONS @custom_settings = {} @batch_actions= [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_items ⇒ Object
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 |
#actions ⇒ Object
Returns the value of attribute actions.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def actions @actions end |
#batch_actions ⇒ Object
Returns the value of attribute batch_actions.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def batch_actions @batch_actions end |
#chart ⇒ Object
Returns the value of attribute chart.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def chart @chart end |
#custom_actions ⇒ Object
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_settings ⇒ Object
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_items ⇒ Object
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 |
#export ⇒ Object
Returns the value of attribute export.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def export @export end |
#form ⇒ Object
Returns the value of attribute form.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def form @form end |
#map ⇒ Object
Returns the value of attribute map.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def map @map end |
#modal_form ⇒ Object
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 |
#model ⇒ Object
Returns the value of attribute model.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def model @model end |
#parent_resources ⇒ Object
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_params ⇒ Object
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_path ⇒ Object
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_items ⇒ Object
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 |
#scopes ⇒ Object
Returns the value of attribute scopes.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def scopes @scopes end |
#search ⇒ Object
Returns the value of attribute search.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def search @search end |
#show ⇒ Object
Returns the value of attribute show.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def show @show end |
#stats ⇒ Object
Returns the value of attribute stats.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def stats @stats end |
#table ⇒ Object
Returns the value of attribute table.
9 10 11 |
# File 'lib/ab_admin/abstract_resource.rb', line 9 def table @table end |
#tree_node_renderer ⇒ Object
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
97 98 99 100 101 102 103 104 |
# File 'lib/ab_admin/abstract_resource.rb', line 97 def action_item(*args, &block) = args. if block_given? instance.action_items << AbAdmin::Config::ActionItem.new(, &block) elsif args[1].is_a?(FalseClass) instance.disabled_action_items << args[0] end end |
.actions(*actions_to_keep) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ab_admin/abstract_resource.rb', line 67 def actions(*actions_to_keep) instance.actions = begin = actions_to_keep. if [:except] ACTIONS - Array([:except]).map(&:to_sym) else actions_to_keep end << :custom_action end.map(&:to_sym) end |
.batch_action(name, options = {}, &block) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/ab_admin/abstract_resource.rb', line 89 def batch_action(name, ={}, &block) if instance.batch_actions << AbAdmin::Config::BatchAction.new(name.to_sym, , &block) else instance.batch_actions.reject!{|a| a.name == name.to_sym } end end |
.belongs_to(*args) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/ab_admin/abstract_resource.rb', line 118 def belongs_to(*args) = args. args.each do |name| instance.parent_resources << OpenStruct.new(name: name, options: ) end end |
.collection_action(name, options = {}, &block) ⇒ Object
129 130 131 |
# File 'lib/ab_admin/abstract_resource.rb', line 129 def collection_action(name, ={}, &block) instance.custom_actions << AbAdmin::Config::CustomAction.new(name, .merge(collection: true), &block) end |
.export(options = {}, &block) ⇒ Object
43 44 45 |
# File 'lib/ab_admin/abstract_resource.rb', line 43 def export(={}, &block) instance.export = ::AbAdmin::Config::Export.new(, &block) end |
.form(options = {}, &block) ⇒ Object
47 48 49 |
# File 'lib/ab_admin/abstract_resource.rb', line 47 def form(={}, &block) instance.form = ::AbAdmin::Config::Form.new(, &block) end |
.member_action(name, options = {}, &block) ⇒ Object
125 126 127 |
# File 'lib/ab_admin/abstract_resource.rb', line 125 def member_action(name, ={}, &block) instance.custom_actions << AbAdmin::Config::CustomAction.new(name, , &block) end |
.modal_form(options = {}, &block) ⇒ Object
51 52 53 |
# File 'lib/ab_admin/abstract_resource.rb', line 51 def modal_form(={}, &block) instance.modal_form = ::AbAdmin::Config::ModalForm.new(, &block) end |
.permitted_params(*values, &block) ⇒ Object
63 64 65 |
# File 'lib/ab_admin/abstract_resource.rb', line 63 def permitted_params(*values, &block) instance.permitted_params = block_given? ? block : values end |
.preview_path(value = nil, options = {}, &block) ⇒ Object
59 60 61 |
# File 'lib/ab_admin/abstract_resource.rb', line 59 def preview_path(value=nil, ={}, &block) instance.preview_path = {value: block_given? ? block : value, options: } end |
.resource_action_item(options = {}, &block) ⇒ Object
110 111 112 |
# File 'lib/ab_admin/abstract_resource.rb', line 110 def resource_action_item(={}, &block) instance.resource_action_items << AbAdmin::Config::ActionItem.new(, &block) end |
.resource_action_items(*actions) ⇒ Object
106 107 108 |
# File 'lib/ab_admin/abstract_resource.rb', line 106 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
133 134 135 |
# File 'lib/ab_admin/abstract_resource.rb', line 133 def scope(name, ={}, &block) instance.scopes << AbAdmin::Config::Scope.new(name, , &block) end |
.search(options = {}, &block) ⇒ Object
39 40 41 |
# File 'lib/ab_admin/abstract_resource.rb', line 39 def search(={}, &block) instance.search = AbAdmin::Config::Search.new(, &block) end |
.settings(value) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ab_admin/abstract_resource.rb', line 78 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
55 56 57 |
# File 'lib/ab_admin/abstract_resource.rb', line 55 def show(={}, &block) instance.show = ::AbAdmin::Config::Show.new(, &block) end |
.table(options = {}, &block) ⇒ Object
35 36 37 |
# File 'lib/ab_admin/abstract_resource.rb', line 35 def table(={}, &block) instance.table = AbAdmin::Config::Table.new(, &block) end |
.tree(&block) ⇒ Object
114 115 116 |
# File 'lib/ab_admin/abstract_resource.rb', line 114 def tree(&block) instance.tree_node_renderer = block end |
Instance Method Details
#action_items_for(action) ⇒ Object
147 148 149 |
# File 'lib/ab_admin/abstract_resource.rb', line 147 def action_items_for(action) @action_items_for[action] ||= action_items.find_all{|i| i.for_action?(action) } end |
#add_admin_addition_to_model ⇒ Object
29 30 31 32 |
# File 'lib/ab_admin/abstract_resource.rb', line 29 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
142 143 144 145 |
# File 'lib/ab_admin/abstract_resource.rb', line 142 def allow_action?(action) return true unless actions actions.include?(action.to_sym) end |
#custom_action_for(name, context) ⇒ Object
167 168 169 170 171 |
# File 'lib/ab_admin/abstract_resource.rb', line 167 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
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ab_admin/abstract_resource.rb', line 151 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
173 174 175 |
# File 'lib/ab_admin/abstract_resource.rb', line 173 def has_module?(module_constant) model.included_modules.include?(module_constant) end |