Class: ZenAdmin::Core::Resource
- Inherits:
-
Object
- Object
- ZenAdmin::Core::Resource
- Defined in:
- lib/zen_admin/core/resource.rb
Defined Under Namespace
Classes: FieldDSL
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#batch_actions ⇒ Object
Returns the value of attribute batch_actions.
-
#exportable ⇒ Object
Returns the value of attribute exportable.
-
#filters(&block) ⇒ Object
Returns the value of attribute filters.
-
#form_fields ⇒ Object
Returns the value of attribute form_fields.
-
#includes_list ⇒ Object
Returns the value of attribute includes_list.
-
#list_fields ⇒ Object
Returns the value of attribute list_fields.
-
#member_actions ⇒ Object
Returns the value of attribute member_actions.
-
#menu_options ⇒ Object
Returns the value of attribute menu_options.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#resource_label_plural ⇒ Object
Returns the value of attribute resource_label_plural.
-
#resource_label_singular ⇒ Object
Returns the value of attribute resource_label_singular.
-
#scopes_list ⇒ Object
Returns the value of attribute scopes_list.
-
#scopes_proc ⇒ Object
Returns the value of attribute scopes_proc.
-
#search_fields ⇒ Object
Returns the value of attribute search_fields.
-
#show_fields ⇒ Object
Returns the value of attribute show_fields.
-
#soft_delete ⇒ Object
Returns the value of attribute soft_delete.
Instance Method Summary collapse
- #batch_action(name_or_array, label: nil, type: :default, confirm: nil, method: nil, url: nil, handler: nil, &block) ⇒ Object
- #computed_scopes(context = nil) ⇒ Object
- #enable_batch_actions(enabled = true) ⇒ Object
- #exportable! ⇒ Object
- #filter_fields ⇒ Object
- #form(&block) ⇒ Object
- #includes(*args) ⇒ Object
-
#initialize(model) ⇒ Resource
constructor
A new instance of Resource.
- #label(singular = nil, plural = nil) ⇒ Object
- #label_plural ⇒ Object
- #list(&block) ⇒ Object
- #member_action(name_or_array, label: nil, type: :default, icon: nil, confirm: nil, method: nil, url: nil, handler: nil, &block) ⇒ Object
- #menu(visible: true, label: nil, icon: nil, position: 10, group: nil) ⇒ Object
- #model ⇒ Object
- #scope(name_or_proc = nil, label: nil, query: {}, default: false, &block) ⇒ Object
- #search(&block) ⇒ Object
- #show(&block) ⇒ Object
- #translate_if_symbol(val) ⇒ Object
Constructor Details
#initialize(model) ⇒ Resource
Returns a new instance of Resource.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/zen_admin/core/resource.rb', line 6 def initialize(model) @model_name = model.to_s if model.respond_to?(:name) @name = model.name.underscore.pluralize else @name = model.to_s.underscore.pluralize end begin @resource_label_singular = model.model_name.human @resource_label_plural = model.model_name.human.pluralize rescue @resource_label_singular = model.to_s.humanize @resource_label_plural = @resource_label_singular.pluralize end @list_fields = [] @form_fields = [] @show_fields = [] @search_fields = [] @filters = [] @actions = [:index, :new, :create, :edit, :update, :show, :destroy] = { visible: true, label: nil, icon: nil, position: 10 } @includes_list = [] @batch_actions = {} @member_actions = {} @scopes_list = [] @scopes_proc = nil @soft_delete = true @exportable = false end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def actions @actions end |
#batch_actions ⇒ Object
Returns the value of attribute batch_actions.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def batch_actions @batch_actions end |
#exportable ⇒ Object
Returns the value of attribute exportable.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def exportable @exportable end |
#filters(&block) ⇒ Object
Returns the value of attribute filters.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def filters @filters end |
#form_fields ⇒ Object
Returns the value of attribute form_fields.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def form_fields @form_fields end |
#includes_list ⇒ Object
Returns the value of attribute includes_list.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def includes_list @includes_list end |
#list_fields ⇒ Object
Returns the value of attribute list_fields.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def list_fields @list_fields end |
#member_actions ⇒ Object
Returns the value of attribute member_actions.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def member_actions @member_actions end |
#menu_options ⇒ Object
Returns the value of attribute menu_options.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def end |
#model_name ⇒ Object
Returns the value of attribute model_name.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def model_name @model_name end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def name @name end |
#resource_label_plural ⇒ Object
Returns the value of attribute resource_label_plural.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def resource_label_plural @resource_label_plural end |
#resource_label_singular ⇒ Object
Returns the value of attribute resource_label_singular.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def resource_label_singular @resource_label_singular end |
#scopes_list ⇒ Object
Returns the value of attribute scopes_list.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def scopes_list @scopes_list end |
#scopes_proc ⇒ Object
Returns the value of attribute scopes_proc.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def scopes_proc @scopes_proc end |
#search_fields ⇒ Object
Returns the value of attribute search_fields.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def search_fields @search_fields end |
#show_fields ⇒ Object
Returns the value of attribute show_fields.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def show_fields @show_fields end |
#soft_delete ⇒ Object
Returns the value of attribute soft_delete.
4 5 6 |
# File 'lib/zen_admin/core/resource.rb', line 4 def soft_delete @soft_delete end |
Instance Method Details
#batch_action(name_or_array, label: nil, type: :default, confirm: nil, method: nil, url: nil, handler: nil, &block) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zen_admin/core/resource.rb', line 118 def batch_action(name_or_array, label: nil, type: :default, confirm: nil, method: nil, url: nil, handler: nil, &block) if name_or_array.is_a?(Array) name_or_array.each do |config| name = config[:name] @batch_actions[name.to_sym] = { name: name.to_sym, label: config[:label], type: config[:type] || :default, confirm: config[:confirm], method: config[:method], url: config[:url], handler: config[:handler] } end else @batch_actions[name_or_array.to_sym] = { name: name_or_array.to_sym, label: label, type: type, confirm: confirm, method: method, url: url, handler: handler || block } end end |
#computed_scopes(context = nil) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/zen_admin/core/resource.rb', line 73 def computed_scopes(context = nil) results = @scopes_list.dup if @scopes_proc dynamic_results = context ? context.instance_exec(&@scopes_proc) : @scopes_proc.call results += Array(dynamic_results) end results end |
#enable_batch_actions(enabled = true) ⇒ Object
114 115 116 |
# File 'lib/zen_admin/core/resource.rb', line 114 def enable_batch_actions(enabled = true) @batch_actions = {} if enabled && (@batch_actions.nil? || @batch_actions == false) end |
#exportable! ⇒ Object
59 60 61 |
# File 'lib/zen_admin/core/resource.rb', line 59 def exportable! @exportable = true end |
#filter_fields ⇒ Object
163 |
# File 'lib/zen_admin/core/resource.rb', line 163 def filter_fields; @filters || []; end |
#form(&block) ⇒ Object
155 |
# File 'lib/zen_admin/core/resource.rb', line 155 def form(&block); dsl = FieldDSL.new(self, :form_fields); dsl.instance_eval(&block); end |
#includes(*args) ⇒ Object
146 |
# File 'lib/zen_admin/core/resource.rb', line 146 def includes(*args); @includes_list.concat(args); end |
#label(singular = nil, plural = nil) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/zen_admin/core/resource.rb', line 38 def label(singular = nil, plural = nil) if singular @resource_label_singular = singular @resource_label_plural = plural || singular else translate_if_symbol(@resource_label_singular) end end |
#label_plural ⇒ Object
47 48 49 |
# File 'lib/zen_admin/core/resource.rb', line 47 def label_plural translate_if_symbol(@resource_label_plural) end |
#list(&block) ⇒ Object
154 |
# File 'lib/zen_admin/core/resource.rb', line 154 def list(&block); dsl = FieldDSL.new(self, :list_fields); dsl.instance_eval(&block); end |
#member_action(name_or_array, label: nil, type: :default, icon: nil, confirm: nil, method: nil, url: nil, handler: nil, &block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/zen_admin/core/resource.rb', line 85 def member_action(name_or_array, label: nil, type: :default, icon: nil, confirm: nil, method: nil, url: nil, handler: nil, &block) if name_or_array.is_a?(Array) name_or_array.each do |config| name = config[:name] @member_actions[name.to_sym] = { name: name.to_sym, label: config[:label], icon: config[:icon], type: config[:type] || :default, confirm: config[:confirm], method: config[:method], url: config[:url], handler: config[:handler] } end else @member_actions[name_or_array.to_sym] = { name: name_or_array.to_sym, label: label, icon: icon, type: type, confirm: confirm, method: method, url: url, handler: handler || block } end end |
#menu(visible: true, label: nil, icon: nil, position: 10, group: nil) ⇒ Object
148 149 150 |
# File 'lib/zen_admin/core/resource.rb', line 148 def (visible: true, label: nil, icon: nil, position: 10, group: nil) = { visible: visible, label: label, icon: icon, position: position, group: group } end |
#model ⇒ Object
152 |
# File 'lib/zen_admin/core/resource.rb', line 152 def model; @model_name.constantize; end |
#scope(name_or_proc = nil, label: nil, query: {}, default: false, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/zen_admin/core/resource.rb', line 63 def scope(name_or_proc = nil, label: nil, query: {}, default: false, &block) if block_given? @scopes_proc = block elsif name_or_proc.is_a?(Proc) @scopes_proc = name_or_proc else @scopes_list << { name: name_or_proc.to_sym, label: label, query: query, default: default } end end |
#search(&block) ⇒ Object
145 |
# File 'lib/zen_admin/core/resource.rb', line 145 def search(&block); end |
#show(&block) ⇒ Object
153 |
# File 'lib/zen_admin/core/resource.rb', line 153 def show(&block); dsl = FieldDSL.new(self, :show_fields); dsl.instance_eval(&block); end |
#translate_if_symbol(val) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/zen_admin/core/resource.rb', line 51 def translate_if_symbol(val) if val.is_a?(Symbol) I18n.t(val, default: [model.model_name.human, val.to_s.split('.').last.humanize]) else val end end |