Class: AdminIt::Context

Inherits:
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.



84
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
113
# File 'lib/admin_it/context/context.rb', line 84

def initialize(from, params: nil, store: nil, parent_init: false)
  run_callbacks :initialize do
    if from.is_a?(self.class.controller_class)
      @controller = from
    elsif from.is_a?(Context)
      @controller = from.controller
      self.parent = from unless parent_init == true
      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

    if params.nil?
      params = controller.request.query_parameters
    end
    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.



18
19
20
# File 'lib/admin_it/context/context.rb', line 18

def context_name
  @context_name
end

.controller_classObject

Returns the value of attribute controller_class.



19
20
21
# File 'lib/admin_it/context/context.rb', line 19

def controller_class
  @controller_class
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



76
77
78
# File 'lib/admin_it/context/context.rb', line 76

def controller
  @controller
end

#parentObject

Returns the value of attribute parent.



76
77
78
# File 'lib/admin_it/context/context.rb', line 76

def parent
  @parent
end

#templateObject (readonly)

Returns the value of attribute template.



76
77
78
# File 'lib/admin_it/context/context.rb', line 76

def template
  @template
end

#toolbarObject (readonly)

Returns the value of attribute toolbar.



76
77
78
# File 'lib/admin_it/context/context.rb', line 76

def toolbar
  @toolbar
end

#top_menuObject (readonly)

Returns the value of attribute top_menu.



76
77
78
# File 'lib/admin_it/context/context.rb', line 76

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)


52
53
54
# File 'lib/admin_it/context/context.rb', line 52

def self.collection?
  false
end

.confirm_destroy?Boolean

Returns:

  • (Boolean)


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

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

.create(context_name, _resource) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/admin_it/context/context.rb', line 29

def self.create(context_name, _resource)
  fail ArgumentError, 'Wrong resource' unless _resource.is_a?(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)


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

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

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

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

.single?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/admin_it/context/context.rb', line 56

def self.single?
  false
end

.url(context = nil, **params) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/admin_it/context/context.rb', line 64

def self.url(context = nil, **params)
  url = context.nil? ? path : context.path
  params = context.nil? ? params : context.url_params(**params)
  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



192
193
194
195
196
# File 'lib/admin_it/context/context.rb', line 192

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

#child?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/admin_it/context/context.rb', line 188

def child?
  parent.is_a?(Context)
end

#field(name) ⇒ Object



128
129
130
# File 'lib/admin_it/context/context.rb', line 128

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

#fields(scope: :visible) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/admin_it/context/context.rb', line 132

def fields(scope: :visible)
  case scope
  when nil, :all then @fields
  when :visible then @fields.select { |f| f.visible? }
  when :hidden then @fields.select { |f| !f.visible? }
  when :readable then @fields.select { |f| f.readable? }
  when :writable then @fields.select { |f| f.writable? }
  when Field::TYPES then @fields.select { |f| f.type == scope }
  else @fields
  end
end

#layoutObject



154
155
156
# File 'lib/admin_it/context/context.rb', line 154

def layout
  @layout ||= ''
end

#layout=(value) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/admin_it/context/context.rb', line 158

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



124
125
126
# File 'lib/admin_it/context/context.rb', line 124

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

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

#save(**params) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/admin_it/context/context.rb', line 144

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

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



205
206
207
208
# File 'lib/admin_it/context/context.rb', line 205

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

#url_params(**params) ⇒ Object



198
199
200
201
202
203
# File 'lib/admin_it/context/context.rb', line 198

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