Class: AdminIt::Context

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

Direct Known Subclasses

CollectionContext, SingleContext

Constant Summary collapse

CONTEXT_REGEXP =
/\A
  (?<resource_name>[a-zA-Z_][a-zA-Z_0-9]*)\/
  (?<context_name>[a-zA-Z_][a-zA-Z_0-9]*)
  (\((?<identity_value>[a-zA-Z_0-9]+)\))?
\z/x

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, params: nil, store: nil, parent_init: false) ⇒ Context

Returns a new instance of Context.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/admin_it/context/context.rb', line 100

def initialize(from, params: nil, store: nil, parent_init: false)
  @children = []

  run_callbacks :initialize do
    if from.is_a?(self.class.controller_class)
      @controller = from
    elsif from.is_a?(Context)
      @controller = from.controller
      unless parent_init == true
        self.parent = from
        from.instance_variable_get(:@children) << self
      end
      params ||= {}
      store ||= {}
    end

    @fields = self.class.fields(scope: :all).map { |f| f.new }

    if store.nil?
      session = controller.session
      store = session[:admin_it] ||= {}
      store = store[resource.name] ||= {}
      store = store[name] ||= {}
    end

    params = controller.request.params if params.nil?
    params = Hash[params.map { |k, v| [k.to_sym, v] }]

    run_callbacks :load, arguments: { params: params, store: store } do
      load_context unless parent_init == true
    end
  end
end

Class Attribute Details

.context_nameObject (readonly)

Returns the value of attribute context_name.



28
29
30
# File 'lib/admin_it/context/context.rb', line 28

def context_name
  @context_name
end

.controller_classObject

Returns the value of attribute controller_class.



29
30
31
# File 'lib/admin_it/context/context.rb', line 29

def controller_class
  @controller_class
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



92
93
94
# File 'lib/admin_it/context/context.rb', line 92

def controller
  @controller
end

#parentObject

Returns the value of attribute parent.



92
93
94
# File 'lib/admin_it/context/context.rb', line 92

def parent
  @parent
end

#templateObject (readonly)

Returns the value of attribute template.



92
93
94
# File 'lib/admin_it/context/context.rb', line 92

def template
  @template
end

#toolbarObject (readonly)

Returns the value of attribute toolbar.



92
93
94
# File 'lib/admin_it/context/context.rb', line 92

def toolbar
  @toolbar
end

#top_menuObject (readonly)

Returns the value of attribute top_menu.



92
93
94
# File 'lib/admin_it/context/context.rb', line 92

def top_menu
  @top_menu
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

.collection?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/admin_it/context/context.rb', line 63

def self.collection?
  false
end

.confirm_destroy?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/admin_it/context/context.rb', line 59

def self.confirm_destroy?
  @confirm_destroy.nil? ? true : @confirm_destroy == true
end

.create(context_name, _resource) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/admin_it/context/context.rb', line 40

def self.create(context_name, _resource)
  _resource.ensure_instance_of!(Resource)
  base = self
  Class.new(base) do
    @resource = _resource
    @context_name = context_name
    @entity_class = @resource.entity_class
    @confirm_destroy = @resource.confirm_destroy?

    import_data_module(base)

    @fields = Hash[
      _resource.fields(scope: :all).map { |f| [f.field_name, f] }
    ]

    before_configure if respond_to?(:before_configure)
  end
end

.dsl_eval(&block) ⇒ Object Originally defined in module ExtendIt::Dsl

.entity_path?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/admin_it/context/context.rb', line 71

def self.entity_path?
  false
end

.field(name) ⇒ Object Originally defined in module FieldsHolder

.fields(scope: :visible) ⇒ Object Originally defined in module FieldsHolder

.iconObject Originally defined in module Iconed

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

.icon?Boolean Originally defined in module Iconed

Returns:

  • (Boolean)

.inherited_class_reader(*names) ⇒ Object Originally defined in module ExtendIt::Base

.metaclass(&block) ⇒ Object Originally defined in module ExtendIt::Base

.single?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/admin_it/context/context.rb', line 67

def self.single?
  false
end

.url(*args, **params) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/admin_it/context/context.rb', line 75

def self.url(*args, **params)
  context = nil
  args.reject! { |arg| arg.is_a?(Context) ? context = arg : false }
  url = context.nil? ? path(*args) : context.path(*args)
  params = context.nil? ? params : context.url_params(**params)
  if params.key?(:parent) && params[:parent].is_a?(Context)
    params[:parent] = params[:parent].to_link
  end
  unless params.empty?
    url << '?' << params.map { |k, v| "#{k}=#{v}" }.join('&')
    url = URI.escape(url)
  end
  url
end

Instance Method Details

#begin_render(template) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/admin_it/context/context.rb', line 222

def begin_render(template)
  @template = template
  @toolbar = Helpers::Toolbar.new(template)
  unless child?
    @top_menu = Helpers::TopMenu.new(template, class: 'navbar-nav')
  end
end

#child?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/admin_it/context/context.rb', line 218

def child?
  parent.is_a?(Context)
end

#end_render(template) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/admin_it/context/context.rb', line 230

def end_render(template)
  render
  request = AdminIt::Request.get(controller.request)
  request["admin_it_#{resource.name}_toolbar"] = template.capture do
    @toolbar.render
  end
  unless child?
    request['admin_it_top_menu'] = template.capture { @top_menu.render }
#        @children.each { |c| c.end_render(template) }
  end
end

#field(name) ⇒ Object



168
169
170
171
# File 'lib/admin_it/context/context.rb', line 168

def field(name)
  name = name.ensure_symbol
  @fields.find { |f| f.name == name }
end

#fields(scope: :visible) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/admin_it/context/context.rb', line 147

def fields(scope: :visible)
  values = @fields
  if scope.is_a?(Hash)
    if scope.key?(:editor)
      return values.select { |f| f.editor == scope[:editor] }
    end
  end
  case scope
  when nil, :all then values
  when :visible then values.select { |f| f.visible? }
  when :hidden then values.select { |f| !f.visible? }
  when :readable then values.select { |f| f.readable? }
  when :writable then values.select { |f| f.writable? }
  when :sortable then values.select { |f| f.sortable? }
  when :with_labels then values.select { |f| f.show_label? }
  when :without_labels then values.select { |f| !f.show_label? }
  when *Field::TYPES then values.select { |f| f.type == scope }
  else values
  end
end

#layoutObject



183
184
185
# File 'lib/admin_it/context/context.rb', line 183

def layout
  @layout ||= ''
end

#layout=(value) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/admin_it/context/context.rb', line 187

def layout=(value)
  value = value.to_sym if value.is_a?(String)
  return unless value.is_a?(Symbol)
  @layout =
    case value
    when :dialog then 'dialog'
    else ''
    end
end

#nameObject



143
144
145
# File 'lib/admin_it/context/context.rb', line 143

def name
  @name ||= self.class.context_name
end

#partial(name, **locals) ⇒ Object



258
259
260
# File 'lib/admin_it/context/context.rb', line 258

def partial(name, **locals)
  Partial.new(name, **locals)
end

#renderObject



242
243
244
245
246
# File 'lib/admin_it/context/context.rb', line 242

def render()
  func = self.class.instance_variable_get(:@render)
  return if func.nil?
  instance_exec(self, &func)
end

#run_callbacks(*names, arguments: [], original_context: false) ⇒ Object Originally defined in module ExtendIt::Callbacks

#save(**params) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/admin_it/context/context.rb', line 173

def save(**params)
  return if controller.nil?
  session = controller.session
  store = session[:admin_it] ||= {}
  store = store[resource.name] ||= {}
  run_callbacks :save, arguments: [{ params: params }] do
    store[name] = params
  end
end


262
263
264
# File 'lib/admin_it/context/context.rb', line 262

def to_link
  "#{resource.name}/#{name}"
end

#url_for(*args, **params) ⇒ Object



253
254
255
256
# File 'lib/admin_it/context/context.rb', line 253

def url_for(*args, **params)
  return nil if @template.nil?
  @template.url_for(*args, url_params(**params))
end

#url_params(**params) ⇒ Object



248
249
250
251
# File 'lib/admin_it/context/context.rb', line 248

def url_params(**params)
  params.merge!(parent: @parent.to_link) unless @parent.nil?
  params
end