Class: AdminIt::Context
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.
99
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
|
# File 'lib/admin_it/context/context.rb', line 99
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_name ⇒ Object
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_class ⇒ Object
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
#controller ⇒ Object
Returns the value of attribute controller.
91
92
93
|
# File 'lib/admin_it/context/context.rb', line 91
def controller
@controller
end
|
#parent ⇒ Object
Returns the value of attribute parent.
91
92
93
|
# File 'lib/admin_it/context/context.rb', line 91
def parent
@parent
end
|
#template ⇒ Object
Returns the value of attribute template.
91
92
93
|
# File 'lib/admin_it/context/context.rb', line 91
def template
@template
end
|
Returns the value of attribute toolbar.
91
92
93
|
# File 'lib/admin_it/context/context.rb', line 91
def toolbar
@toolbar
end
|
Returns the value of attribute top_menu.
91
92
93
|
# File 'lib/admin_it/context/context.rb', line 91
def
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
62
63
64
|
# File 'lib/admin_it/context/context.rb', line 62
def self.collection?
false
end
|
.confirm_destroy? ⇒ Boolean
58
59
60
|
# File 'lib/admin_it/context/context.rb', line 58
def self.confirm_destroy?
@confirm_destroy.nil? ? true : @confirm_destroy == true
end
|
.create(context_name, _resource) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/admin_it/context/context.rb', line 39
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
70
71
72
|
# File 'lib/admin_it/context/context.rb', line 70
def self.entity_path?
false
end
|
.field(name) ⇒ Object
Originally defined in module
FieldsHolder
.fields(scope: :visible) ⇒ Object
Originally defined in module
FieldsHolder
.icon ⇒ Object
Originally defined in module
Iconed
.icon=(value) ⇒ Object
Originally defined in module
Iconed
.icon? ⇒ Boolean
Originally defined in module
Iconed
.inherited_class_reader(*names) ⇒ Object
Originally defined in module
ExtendIt::Base
.single? ⇒ Boolean
66
67
68
|
# File 'lib/admin_it/context/context.rb', line 66
def self.single?
false
end
|
.url(*args, **params) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/admin_it/context/context.rb', line 74
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?
= Helpers::.new(template, class: 'navbar-nav')
end
end
|
#child? ⇒ 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
|
# File 'lib/admin_it/context/context.rb', line 230
def end_render(template)
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 { .render }
end
end
|
#field(name) ⇒ Object
167
168
169
170
|
# File 'lib/admin_it/context/context.rb', line 167
def field(name)
name = name.ensure_symbol
@fields.find { |f| f.name == name }
end
|
#fields(scope: :visible) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/admin_it/context/context.rb', line 146
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
|
#layout ⇒ Object
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
|
#name ⇒ Object
142
143
144
|
# File 'lib/admin_it/context/context.rb', line 142
def name
@name ||= self.class.context_name
end
|
#partial(name, **locals) ⇒ Object
251
252
253
|
# File 'lib/admin_it/context/context.rb', line 251
def partial(name, **locals)
Partial.new(name, **locals)
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
|
#to_link ⇒ Object
255
256
257
|
# File 'lib/admin_it/context/context.rb', line 255
def to_link
"#{resource.name}/#{name}"
end
|
#url_for(*args, **params) ⇒ Object
246
247
248
249
|
# File 'lib/admin_it/context/context.rb', line 246
def url_for(*args, **params)
return nil if @template.nil?
@template.url_for(*args, url_params(**params))
end
|
#url_params(**params) ⇒ Object
241
242
243
244
|
# File 'lib/admin_it/context/context.rb', line 241
def url_params(**params)
params.merge!(parent: @parent.to_link) unless @parent.nil?
params
end
|