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.
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 103
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.
95
96
97
|
# File 'lib/admin_it/context/context.rb', line 95
def controller
@controller
end
|
#parent ⇒ Object
Returns the value of attribute parent.
95
96
97
|
# File 'lib/admin_it/context/context.rb', line 95
def parent
@parent
end
|
#template ⇒ Object
Returns the value of attribute template.
95
96
97
|
# File 'lib/admin_it/context/context.rb', line 95
def template
@template
end
|
Returns the value of attribute toolbar.
95
96
97
|
# File 'lib/admin_it/context/context.rb', line 95
def toolbar
@toolbar
end
|
Returns the value of attribute top_menu.
95
96
97
|
# File 'lib/admin_it/context/context.rb', line 95
def
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
63
64
65
|
# File 'lib/admin_it/context/context.rb', line 63
def self.collection?
false
end
|
.create(context_name, _resource, &block) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/admin_it/context/context.rb', line 31
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
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
71
72
73
|
# File 'lib/admin_it/context/context.rb', line 71
def self.entity_path?
false
end
|
.field(*names, field_class: nil, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/admin_it/context/context.rb', line 51
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
67
68
69
|
# File 'lib/admin_it/context/context.rb', line 67
def self.single?
false
end
|
.url(context = nil, **params) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/admin_it/context/context.rb', line 75
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
211
212
213
214
215
|
# File 'lib/admin_it/context/context.rb', line 211
def begin_render(template)
@template = template
@toolbar = Helpers::Toolbar.new(template)
= Helpers::.new(template, class: 'navbar-nav')
end
|
#child? ⇒ Boolean
207
208
209
|
# File 'lib/admin_it/context/context.rb', line 207
def child?
parent.is_a?(Context)
end
|
#field(name) ⇒ Object
147
148
149
|
# File 'lib/admin_it/context/context.rb', line 147
def field(name)
@fields.find { |f| f.name == name }
end
|
#fields(scope: :visible) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/admin_it/context/context.rb', line 151
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
173
174
175
|
# File 'lib/admin_it/context/context.rb', line 173
def layout
@layout ||= ''
end
|
#layout=(value) ⇒ Object
177
178
179
180
181
182
183
184
|
# File 'lib/admin_it/context/context.rb', line 177
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
143
144
145
|
# File 'lib/admin_it/context/context.rb', line 143
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
163
164
165
166
167
168
169
170
171
|
# File 'lib/admin_it/context/context.rb', line 163
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
217
218
219
220
221
222
|
# File 'lib/admin_it/context/context.rb', line 217
def url_params(**params)
unless @parent.nil?
params.merge!(parent: @parent.send(:context_param))
end
params
end
|