Class: AdminIt::Resource
- Extended by:
- ExtendIt::Base
- Includes:
- FieldsHolder, FiltersHolder, Iconed, ExtendIt::Callbacks, ExtendIt::Dsl
- Defined in:
- lib/admin_it/resource.rb
Instance Attribute Summary collapse
-
#entity_class ⇒ Object
readonly
Returns the value of attribute entity_class.
-
#menu ⇒ Object
readonly
Returns the value of attribute menu.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#plural ⇒ Object
readonly
Returns the value of attribute plural.
Class Method Summary collapse
- .attr_checker(*names) ⇒ Object extended from ExtendIt::Base
- .call_inherited(method_name, *args, base_first: false, &block) ⇒ Object extended from ExtendIt::Base
- .class_attr_reader(*attrs) ⇒ Object extended from ExtendIt::Base
- .inherited_class_reader(*names) ⇒ Object extended from ExtendIt::Base
- .metaclass(&block) ⇒ Object extended from ExtendIt::Base
Instance Method Summary collapse
- #[](name) ⇒ Object
- #collection_path ⇒ Object
- #collections ⇒ Object
- #confirm_destroy? ⇒ Boolean
- #context(name) ⇒ Object
- #contexts ⇒ Object
- #contexts_names ⇒ Object
- #default_context(value = nil) ⇒ Object
- #define_controller ⇒ Object
- #destroyable? ⇒ Boolean
- #display_name ⇒ Object
- #dsl_eval(&block) ⇒ Object included from ExtendIt::Dsl
- #field(name) ⇒ Object included from FieldsHolder
- #fields(scope: :visible) ⇒ Object included from FieldsHolder
- #filter(name) ⇒ Object included from FiltersHolder
- #filters(scope: :all) ⇒ Object included from FiltersHolder
- #icon ⇒ Object included from Iconed
- #icon=(value) ⇒ Object included from Iconed
-
#initialize(name, entity_class = nil, menu: true, destroyable: true, auto_filters: true) ⇒ Resource
constructor
A new instance of Resource.
- #run_callbacks(*names, arguments: [], original_context: false) ⇒ Object included from ExtendIt::Callbacks
- #single_path(entity) ⇒ Object
- #singles ⇒ Object
Constructor Details
#initialize(name, entity_class = nil, menu: true, destroyable: true, auto_filters: true) ⇒ Resource
Returns a new instance of Resource.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/admin_it/resource.rb', line 52 def initialize( name, entity_class = nil, menu: true, destroyable: true, auto_filters: true ) name = name.ensure_symbol || fail( ArgumentError, '`name` argument for resource constructor should be a Symbol ' \ 'or a String' ) @name, @entity_class = name, entity_class if @entity_class.nil? begin @entity_class = Object.const_get(name.to_s.camelize) # !PORTABLE rescue NameError fail ArgumentError, "Can't find entity class for #{name}" end end import_data_module run_callbacks :initialize do @fields = Hash[default_fields.map { |f| [f.field_name, f] }] @filters = if auto_filters Hash[default_filters.map { |f| [f.filter_name, f] }] else {} end @contexts = Hash[default_contexts.map { |c| [c.context_name, c] }] = == true @destroyable = destroyable == true @plural = name.to_s.pluralize # !POTABLE @default_context = nil end end |
Instance Attribute Details
#entity_class ⇒ Object (readonly)
Returns the value of attribute entity_class.
48 49 50 |
# File 'lib/admin_it/resource.rb', line 48 def entity_class @entity_class end |
#menu ⇒ Object (readonly)
Returns the value of attribute menu.
48 49 50 |
# File 'lib/admin_it/resource.rb', line 48 def end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
48 49 50 |
# File 'lib/admin_it/resource.rb', line 48 def name @name end |
#plural ⇒ Object (readonly)
Returns the value of attribute plural.
48 49 50 |
# File 'lib/admin_it/resource.rb', line 48 def plural @plural end |
Class Method Details
.attr_checker(*names) ⇒ Object Originally defined in module ExtendIt::Base
.call_inherited(method_name, *args, base_first: false, &block) ⇒ Object Originally defined in module ExtendIt::Base
.class_attr_reader(*attrs) ⇒ Object Originally defined in module ExtendIt::Base
.inherited_class_reader(*names) ⇒ Object Originally defined in module ExtendIt::Base
.metaclass(&block) ⇒ Object Originally defined in module ExtendIt::Base
Instance Method Details
#[](name) ⇒ Object
103 104 105 |
# File 'lib/admin_it/resource.rb', line 103 def [](name) context(name) end |
#collection_path ⇒ Object
132 133 134 |
# File 'lib/admin_it/resource.rb', line 132 def collection_path AdminIt::Engine.routes.url_helpers.send("#{plural}_path") end |
#collections ⇒ Object
140 141 142 |
# File 'lib/admin_it/resource.rb', line 140 def collections contexts.select { |c| c.collection? } end |
#confirm_destroy? ⇒ Boolean
95 96 97 |
# File 'lib/admin_it/resource.rb', line 95 def confirm_destroy? @confirm_destroy.nil? ? @confirm_destroy = true : @confirm_destroy == true end |
#context(name) ⇒ Object
107 108 109 |
# File 'lib/admin_it/resource.rb', line 107 def context(name) @contexts[name.ensure_symbol] end |
#contexts ⇒ Object
111 112 113 |
# File 'lib/admin_it/resource.rb', line 111 def contexts @contexts.values end |
#contexts_names ⇒ Object
124 125 126 |
# File 'lib/admin_it/resource.rb', line 124 def contexts_names @contexts.map(&:context_name) end |
#default_context(value = nil) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/admin_it/resource.rb', line 115 def default_context(value = nil) return @default_context unless @default_context.nil? if collections.size > 0 @default_context = collections.first.context_name elsif singles.size > 0 @default_context = singles.first.context_name end end |
#define_controller ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/admin_it/resource.rb', line 148 def define_controller c_name = "#{name.to_s.camelize}Controller" # !PORTABLE resource = self c_class = Class.new(AdminIt.config.controller) do @resource = resource include AdminIt::Controller resource.contexts.each do |_context| define_method(_context.context_name) { load_context(_context) } if _context < SavableSingleContext define_method _context.save_action do load_context(_context) { context.save_entity } end end end if resource.destroyable? define_method :destroy do load_context(resource[:show]) { context.destroy_entity } end end end AdminIt.const_set(c_name, c_class) contexts.each { |c| c.controller_class = c_class } end |
#destroyable? ⇒ Boolean
99 100 101 |
# File 'lib/admin_it/resource.rb', line 99 def destroyable? @destroyable.nil? ? @destroyable = true : @destroyable == true end |
#display_name ⇒ Object
128 129 130 |
# File 'lib/admin_it/resource.rb', line 128 def display_name @display_name ||= plural.split('_').map { |s| s.capitalize }.join(' ') end |
#dsl_eval(&block) ⇒ Object Originally defined in module ExtendIt::Dsl
#field(name) ⇒ Object Originally defined in module FieldsHolder
#fields(scope: :visible) ⇒ Object Originally defined in module FieldsHolder
#filter(name) ⇒ Object Originally defined in module FiltersHolder
#filters(scope: :all) ⇒ Object Originally defined in module FiltersHolder
#run_callbacks(*names, arguments: [], original_context: false) ⇒ Object Originally defined in module ExtendIt::Callbacks
#single_path(entity) ⇒ Object
136 137 138 |
# File 'lib/admin_it/resource.rb', line 136 def single_path(entity) AdminIt::Engine.routes.url_helpers.send("#{name}_path", entity) end |
#singles ⇒ Object
144 145 146 |
# File 'lib/admin_it/resource.rb', line 144 def singles contexts.select { |c| c.single? } end |