Class: AdminIt::Resource

Inherits:
Object show all
Extended by:
ExtendIt::Base
Includes:
FieldsHolder, FiltersHolder, Iconed, ExtendIt::Callbacks, ExtendIt::Dsl
Defined in:
lib/admin_it/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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] }]

    @menu = menu == true
    @destroyable = destroyable == true
    @plural = name.to_s.pluralize # !POTABLE
    @default_context = nil
  end
end

Instance Attribute Details

#entity_classObject (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

Returns the value of attribute menu.



48
49
50
# File 'lib/admin_it/resource.rb', line 48

def menu
  @menu
end

#nameObject (readonly)

Returns the value of attribute name.



48
49
50
# File 'lib/admin_it/resource.rb', line 48

def name
  @name
end

#pluralObject (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_pathObject



132
133
134
# File 'lib/admin_it/resource.rb', line 132

def collection_path
  AdminIt::Engine.routes.url_helpers.send("#{plural}_path")
end

#collectionsObject



140
141
142
# File 'lib/admin_it/resource.rb', line 140

def collections
  contexts.select { |c| c.collection? }
end

#confirm_destroy?Boolean

Returns:

  • (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

#contextsObject



111
112
113
# File 'lib/admin_it/resource.rb', line 111

def contexts
  @contexts.values
end

#contexts_namesObject



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_controllerObject



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

Returns:

  • (Boolean)


99
100
101
# File 'lib/admin_it/resource.rb', line 99

def destroyable?
  @destroyable.nil? ? @destroyable = true : @destroyable == true
end

#display_nameObject



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

#iconObject Originally defined in module Iconed

#icon=(value) ⇒ Object Originally defined in module Iconed

#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

#singlesObject



144
145
146
# File 'lib/admin_it/resource.rb', line 144

def singles
  contexts.select { |c| c.single? }
end