Class: Qor::Layout::Setting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Qor::Layout::Setting
show all
- Defined in:
- app/models/qor/layout/setting.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
143
144
145
146
147
148
|
# File 'app/models/qor/layout/setting.rb', line 143
def method_missing(method_sym, *args, &block)
if method_sym.to_s =~ /^value_attributes\[(\w+)\]$/
return self.values($1)
end
super
end
|
Instance Attribute Details
#destroy_value_attributes ⇒ Object
Returns the value of attribute destroy_value_attributes.
5
6
7
|
# File 'app/models/qor/layout/setting.rb', line 5
def destroy_value_attributes
@destroy_value_attributes
end
|
Instance Method Details
#assign_serialized_attributes ⇒ Object
20
21
22
23
24
25
26
27
28
29
|
# File 'app/models/qor/layout/setting.rb', line 20
def assign_serialized_attributes
self.value = @value_attributes unless @value_attributes.nil?
self.style = @style_attributes unless @style_attributes.nil?
new_value = self.value
(self.destroy_value_attributes || {}).map do |key, value|
new_value = new_value.reject {|k,v| k.to_s == key.to_s } if value == "1"
end
self.value = new_value
end
|
#children_attributes=(attrs) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'app/models/qor/layout/setting.rb', line 85
def children_attributes=(attrs)
attrs.map do |key, atts|
child = children.find_by_id(atts.delete("id"))
if atts.delete("_destroy").to_s == "1"
child.try(:destroy)
elsif child
child.update_attributes(atts)
else
child_attrs = atts['value_attributes']
children.new(atts) if child_attrs.keys.any? {|k| child_attrs[k].present? }
end
end
end
|
#gadget ⇒ Object
135
136
137
|
# File 'app/models/qor/layout/setting.rb', line 135
def gadget
Qor::Layout::Configuration.first(:gadget, name)
end
|
#gadget_settings ⇒ Object
139
140
141
|
# File 'app/models/qor/layout/setting.rb', line 139
def gadget_settings
gadget.first(:settings)
end
|
46
47
48
49
50
51
|
# File 'app/models/qor/layout/setting.rb', line 46
def meta_settings
gadget_settings.children.inject({}) do |s, setting|
value = values(setting.name, true)
s.merge({setting.name => value})
end.with_indifferent_access
end
|
53
54
55
|
# File 'app/models/qor/layout/setting.rb', line 53
def related_records
settings.respond_to?(:values) ? settings.values.select {|x| x.is_a? ActiveRecord::Base } : []
end
|
#render(edit_mode = false) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'app/models/qor/layout/setting.rb', line 120
def render(edit_mode=false)
parse_content = Nokogiri::HTML::DocumentFragment.parse(render_without_style)
old_style_css = parse_content.xpath('*').first.attribute("style").to_s
old_style = old_style_css.split(";").inject({}) {|s, v| s.merge(Hash[*v.split(":").map(&:strip)]) }
new_style = old_style.merge(style)
style_css = new_style.map {|k,v| "#{k}: #{v}"}.join("; ")
parse_content.xpath('*').first.set_attribute("qor_layout_elements", id.to_s)
parse_content.xpath('*').first.set_attribute("qor_layout_draggable_elements", id.to_s) if gadget.options[:floating]
parse_content.xpath('*').first.set_attribute("style", style_css)
= edit_mode ? "<div for_qor_layout_elements='#{id}'><a href='/admin/layout_settings/#{id}/edit'><img src='/qor_widget/images/settings.png'/></a></div>" : ""
parse_content.to_xhtml +
end
|
#render_without_style ⇒ Object
116
117
118
|
# File 'app/models/qor/layout/setting.rb', line 116
def render_without_style
::Mustache.render(gadget.first(:template).try(:value).to_s, settings)
end
|
#resource_attributes_for_settings ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'app/models/qor/layout/setting.rb', line 107
def resource_attributes_for_settings
attrs = []
gadget_settings.children.map do |child|
attr_show = !child.options[:hidden]
attrs << Qor::ResourceAttribute.new("value_attributes[#{child.name}]", child.options) if attr_show
end
attrs
end
|
#settings ⇒ Object
99
100
101
102
103
104
105
|
# File 'app/models/qor/layout/setting.rb', line 99
def settings
if gadget.first(:context).try(:block)
self.instance_eval &(gadget.first(:context).try(:block))
else
meta_settings
end
end
|
#style_attributes=(attrs) ⇒ Object
79
80
81
82
83
|
# File 'app/models/qor/layout/setting.rb', line 79
def style_attributes=(attrs)
old_style = self.style.symbolize_keys
new_style = old_style.merge(attrs.symbolize_keys || {})
@style_attributes = new_style
end
|
#value_attributes=(attrs) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'app/models/qor/layout/setting.rb', line 57
def value_attributes=(attrs)
attrs = attrs.with_indifferent_access
value_attrs = {}
gadget_settings.find(:meta).map do |child|
key = child.name
if child.options[:type].to_s =~ /image|file|media/
if self.value && (self.value[key] =~ /^([\w:]+)\((\d+)\)$/)
asset = $1.constantize.find_by_id($2).update_attribute(:data, attrs[key]) if attrs[key]
value_attrs.update({key => self.value[key]})
elsif attrs[key]
asset = Qor::Layout::Asset.create(:data => attrs[key])
value_attrs.update({key => "Qor::Layout::Asset(#{asset.id})"})
end
elsif attrs[key]
value_attrs.update({key => attrs[key]})
end
end
old_value = (self.value || {}).symbolize_keys
@value_attributes = old_value.merge(value_attrs.symbolize_keys)
end
|
#values(name, for_setting = false) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/models/qor/layout/setting.rb', line 31
def values(name, for_setting=false)
stored_value = (value || {}).with_indifferent_access[name]
gadget_setting = gadget_settings.first(:meta, name)
if stored_value =~ /^([\w:]+)\((\d+)\)$/
return ($1.constantize.find_by_id($2) rescue stored_value)
elsif gadget_setting && (gadget_setting.options[:type].to_s == 'gadget')
gadget_name = gadget_setting.options[:name] || name
gadgets = children.where(:name => gadget_name)
return (for_setting ? gadgets.map(&:settings) : gadgets)
else
return stored_value
end
end
|