Module: CustomAttributes::ActsAsCustomizable::InstanceMethods
- Defined in:
- lib/custom_attributes/acts_as/acts_as_customizable.rb
Instance Method Summary collapse
-
#assign_custom_values=(values) ⇒ Object
Sets the values of the object’s custom fields values is an array like [=> 1, ‘value’ => ‘foo’, => 2, ‘value’ => ‘bar’].
-
#available_custom_fields ⇒ Object
Helper function to access available custom fields from an instance.
-
#custom_field_value(c) ⇒ Object
Returns the value for the passed CustomField object or ID.
-
#custom_field_values ⇒ Object
Accessor for custom fields, returns array of CustomFieldValues.
-
#custom_field_values=(values) ⇒ Object
Sets the values of the object’s custom fields values is a hash like => ‘foo’, 2 => ‘bar’.
- #custom_field_values_changed? ⇒ Boolean
-
#custom_value_for(c) ⇒ Object
Returns a CustomValue object for the passed CustomField object or ID.
- #populate_custom_field_value(field) ⇒ Object
- #populated_custom_field_value(c) ⇒ Object
- #reassign_custom_field_values ⇒ Object
- #reload(*args) ⇒ Object
- #reset_custom_values! ⇒ Object
-
#save_custom_field_values ⇒ Object
Called after save of the extended model, so validation should already be over.
-
#use_custom_value_json(hash = {}) ⇒ Object
Set JSON representation in elasticsearch.
-
#validate_custom_field_values ⇒ Object
Extends model validation 1.
- #visible_custom_field_values ⇒ Object
-
#visible_in_search ⇒ Object
Override this to have control over entity visibility in search.
Instance Method Details
#assign_custom_values=(values) ⇒ Object
Sets the values of the object’s custom fields values is an array like [=> 1, ‘value’ => ‘foo’, => 2, ‘value’ => ‘bar’]
76 77 78 79 80 81 82 83 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 76 def assign_custom_values=(values) values_to_hash = values.each_with_object({}) do |v, hash| v = v.stringify_keys hash[v['id']] = v['value'] if v['id'] && v.key?('value') end self.custom_field_values = values_to_hash end |
#available_custom_fields ⇒ Object
Helper function to access available custom fields from an instance
64 65 66 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 64 def available_custom_fields self.class.available_custom_fields end |
#custom_field_value(c) ⇒ Object
Returns the value for the passed CustomField object or ID
153 154 155 156 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 153 def custom_field_value(c) field_id = (c.is_a?(CustomField) ? c.id : c.to_i) custom_field_values.detect { |v| v.custom_field_id == field_id }.try(:value) end |
#custom_field_values ⇒ Object
Accessor for custom fields, returns array of CustomFieldValues
105 106 107 108 109 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 105 def custom_field_values @_custom_field_values ||= available_custom_fields.collect do |field| populate_custom_field_value(field) end end |
#custom_field_values=(values) ⇒ Object
Sets the values of the object’s custom fields values is a hash like => ‘foo’, 2 => ‘bar’
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 87 def custom_field_values=(values) values = values.stringify_keys custom_field_values.each do |custom_field_value| key = custom_field_value.custom_field_id.to_s slug = custom_field_value.custom_field_slug if values.key?(key) custom_field_value.value = values[key] elsif values.key?(slug) custom_field_value.value = values[slug] end end @custom_field_values_changed = true end |
#custom_field_values_changed? ⇒ Boolean
142 143 144 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 142 def custom_field_values_changed? @custom_field_values_changed == true end |
#custom_value_for(c) ⇒ Object
Returns a CustomValue object for the passed CustomField object or ID
147 148 149 150 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 147 def custom_value_for(c) field_id = (c.is_a?(CustomField) ? c.id : c.to_i) custom_values.detect { |v| v.custom_field_id == field_id } end |
#populate_custom_field_value(field) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 119 def populate_custom_field_value(field) x = CustomAttributes::CustomFieldValue.new x.custom_field = field x.customizable = self if field.multiple? values = custom_values.select { |v| v.custom_field == field } if values.empty? values << custom_values.build(customizable: self, custom_field: field) end x.instance_variable_set('@value', values.map(&:value)) else cv = custom_values.detect { |v| v.custom_field == field } cv ||= custom_values.build(customizable: self, custom_field: field) x.instance_variable_set('@value', cv.value) end x.value_was = x.value.dup if x.value x end |
#populated_custom_field_value(c) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 111 def populated_custom_field_value(c) field_id = (c.is_a?(CustomField) ? c.id : c.to_i) field = available_custom_fields.select { |field| field.id == field_id }.first return populate_custom_field_value(field) unless field.nil? CustomAttributes::CustomFieldValue.new end |
#reassign_custom_field_values ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 196 def reassign_custom_field_values if @_custom_field_values values = @_custom_field_values.each_with_object({}) { |v, h| h[v.custom_field_id] = v.value; } @_custom_field_values = nil self.custom_field_values = values end end |
#reload(*args) ⇒ Object
209 210 211 212 213 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 209 def reload(*args) @_custom_field_values = nil @custom_field_values_changed = false super end |
#reset_custom_values! ⇒ Object
204 205 206 207 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 204 def reset_custom_values! @_custom_field_values = nil @custom_field_values_changed = true end |
#save_custom_field_values ⇒ Object
Called after save of the extended model, so validation should already be over. This method is responsible for persisting the values that have been written to the CustomFieldValues and handle and save CustomValues correctly.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 173 def save_custom_field_values target_custom_values = [] custom_field_values.each do |custom_field_value| if custom_field_value.value.is_a?(Array) custom_field_value.value.each do |v| target = custom_values.detect { |cv| cv.custom_field == custom_field_value.custom_field && cv.value == v } target ||= custom_values.build(customizable: self, custom_field: custom_field_value.custom_field, value: v) target_custom_values << target end else target = custom_values.detect { |cv| cv.custom_field == custom_field_value.custom_field } target ||= custom_values.build(customizable: self, custom_field: custom_field_value.custom_field) target.value = custom_field_value.value target_custom_values << target end end self.custom_values = target_custom_values custom_values.each(&:save) @custom_field_values_changed = false true end |
#use_custom_value_json(hash = {}) ⇒ Object
Set JSON representation in elasticsearch. Default is to decorate the models JSON presentation with custom values
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 49 def use_custom_value_json(hash = {}) to_json = { methods: :visible_in_search, include: { custom_field_values: { only: %i[custom_field_id value] } } }.merge(hash) as_json( to_json ) end |
#validate_custom_field_values ⇒ Object
Extends model validation
-
Calls .validate_value on each CustomFieldValue
-
.validate_value calls .validate_custom_value on the CustomField,
-
which calls the validate_custom_value method on the assigned FieldType.
The FieldType is therefor responsible for CustomValue validation.
164 165 166 167 168 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 164 def validate_custom_field_values if new_record? || custom_field_values_changed? custom_field_values.each(&:validate_value) end end |
#visible_custom_field_values ⇒ Object
138 139 140 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 138 def visible_custom_field_values custom_field_values.select(&:visible?) end |
#visible_in_search ⇒ Object
Override this to have control over entity visibility in search. Entities that return false here will be filtered out by default.
70 71 72 |
# File 'lib/custom_attributes/acts_as/acts_as_customizable.rb', line 70 def visible_in_search true end |