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
-
.call_inherited(method_name, *args, base_first: false, &block) ⇒ Object
extended
from ExtendIt::Class
-
.class_attr_reader(*attrs) ⇒ Object
extended
from ExtendIt::Class
-
.collection? ⇒ Boolean
-
.create(context_name, _resource, &block) ⇒ Object
-
.dsl_accessor(*names, default: nil, &setter) ⇒ Object
extended
from ExtendIt::Dsl
-
.dsl_block(*names) ⇒ Object
extended
from ExtendIt::Dsl
-
.dsl_boolean(*names, default: true) ⇒ Object
extended
from ExtendIt::Dsl
-
.dsl_use_hash(hash_name) ⇒ Object
extended
from ExtendIt::Dsl
-
.entity_path? ⇒ Boolean
-
.field(*names, field_class: nil, &block) ⇒ Object
-
.fields(scope: :visible) ⇒ Object
extended
from FieldsHolder
-
.hide_fields(*names) ⇒ Object
extended
from FieldsHolder
-
.inherited_class_reader(*names) ⇒ Object
extended
from ExtendIt::Class
-
.show_fields(*names) ⇒ Object
extended
from FieldsHolder
-
.single? ⇒ Boolean
-
.url(context = nil, **params) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(from, params: nil, store: nil, parent_init: false) ⇒ Context
Returns a new instance of Context.
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
133
134
|
# File 'lib/admin_it/context/context.rb', line 105
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_name ⇒ Object
Returns the value of attribute context_name.
19
20
21
|
# File 'lib/admin_it/context/context.rb', line 19
def context_name
@context_name
end
|
.controller_class ⇒ Object
Returns the value of attribute controller_class.
20
21
22
|
# File 'lib/admin_it/context/context.rb', line 20
def controller_class
@controller_class
end
|
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
97
98
99
|
# File 'lib/admin_it/context/context.rb', line 97
def controller
@controller
end
|
#parent ⇒ Object
Returns the value of attribute parent.
97
98
99
|
# File 'lib/admin_it/context/context.rb', line 97
def parent
@parent
end
|
#template ⇒ Object
Returns the value of attribute template.
97
98
99
|
# File 'lib/admin_it/context/context.rb', line 97
def template
@template
end
|
Returns the value of attribute toolbar.
97
98
99
|
# File 'lib/admin_it/context/context.rb', line 97
def toolbar
@toolbar
end
|
Returns the value of attribute top_menu.
97
98
99
|
# File 'lib/admin_it/context/context.rb', line 97
def
@top_menu
end
|
Class Method Details
.call_inherited(method_name, *args, base_first: false, &block) ⇒ Object
Originally defined in module
ExtendIt::Class
.class_attr_reader(*attrs) ⇒ Object
Originally defined in module
ExtendIt::Class
.collection? ⇒ Boolean
65
66
67
|
# File 'lib/admin_it/context/context.rb', line 65
def self.collection?
false
end
|
.create(context_name, _resource, &block) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/admin_it/context/context.rb', line 32
def self.create(context_name, _resource, &block)
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)
instance_eval(&block) if block_given?
after_configure if respond_to?(:after_configure)
end
end
|
.dsl_accessor(*names, default: nil, &setter) ⇒ Object
Originally defined in module
ExtendIt::Dsl
.dsl_block(*names) ⇒ Object
Originally defined in module
ExtendIt::Dsl
.dsl_boolean(*names, default: true) ⇒ Object
Originally defined in module
ExtendIt::Dsl
.dsl_use_hash(hash_name) ⇒ Object
Originally defined in module
ExtendIt::Dsl
.entity_path? ⇒ Boolean
73
74
75
|
# File 'lib/admin_it/context/context.rb', line 73
def self.entity_path?
false
end
|
.field(*names, field_class: nil, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/admin_it/context/context.rb', line 53
def self.field(*names, field_class: nil, &block)
names.ensure_symbols.each do |name|
if @fields.key?(name)
field = @fields[name] = Class.new(@fields[name]) if block_given?
else
field_class = Field if field_class.nil? || !field_class <= Field
field = @fields[name] = field_class.create(name, entity_class)
end
field.instance_eval(&block) if block_given?
end
end
|
.fields(scope: :visible) ⇒ Object
Originally defined in module
FieldsHolder
.hide_fields(*names) ⇒ Object
Originally defined in module
FieldsHolder
.inherited_class_reader(*names) ⇒ Object
Originally defined in module
ExtendIt::Class
.show_fields(*names) ⇒ Object
Originally defined in module
FieldsHolder
.single? ⇒ Boolean
69
70
71
|
# File 'lib/admin_it/context/context.rb', line 69
def self.single?
false
end
|
.url(context = nil, **params) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/admin_it/context/context.rb', line 77
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
213
214
215
216
217
|
# File 'lib/admin_it/context/context.rb', line 213
def begin_render(template)
@template = template
@toolbar = Helpers::Toolbar.new(template)
@top_menu = Helpers::TopMenu.new(template, class: 'navbar-nav')
end
|
#child? ⇒ Boolean
209
210
211
|
# File 'lib/admin_it/context/context.rb', line 209
def child?
parent.is_a?(Context)
end
|
#field(name) ⇒ Object
149
150
151
|
# File 'lib/admin_it/context/context.rb', line 149
def field(name)
@fields.find { |f| f.name == name }
end
|
#fields(scope: :visible) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/admin_it/context/context.rb', line 153
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
|
#layout ⇒ Object
175
176
177
|
# File 'lib/admin_it/context/context.rb', line 175
def layout
@layout ||= ''
end
|
#layout=(value) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/admin_it/context/context.rb', line 179
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
145
146
147
|
# File 'lib/admin_it/context/context.rb', line 145
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
165
166
167
168
169
170
171
172
173
|
# File 'lib/admin_it/context/context.rb', line 165
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_params(**params) ⇒ Object
219
220
221
222
223
224
|
# File 'lib/admin_it/context/context.rb', line 219
def url_params(**params)
unless @parent.nil?
params.merge!(parent: @parent.send(:context_param))
end
params
end
|