Module: CustomFields::TargetHelpers
- Defined in:
- lib/custom_fields/target_helpers.rb
Instance Method Summary collapse
-
#belongs_to_custom_fields ⇒ Array
Return the names of all the belongs_to custom_fields of this object.
-
#custom_fields_basic_attributes ⇒ Hash
Build a hash for all the non-relationship fields meaning string, text, date, boolean, select, file types.
-
#custom_fields_basic_attributes=(attributes) ⇒ Object
Set the values (and their related fields) for all the non-relationship fields meaning string, text, date, boolean, select, file types.
-
#custom_fields_methods(&filter) ⇒ List
Return the list of the getters dynamically based on the custom_fields recipe in order to get the formatted values of the custom fields.
-
#custom_fields_safe_setters ⇒ List
List all the setters that are used by the custom_fields in order to get updated thru a html form for instance.
-
#file_custom_fields ⇒ Array
Return the names of all the file custom_fields of this object.
-
#has_many_custom_fields ⇒ Array
Return the names of all the has_many custom_fields of this object.
-
#is_a_custom_field_many_relationship?(name) ⇒ Boolean
Check if the rule defined by the name is a “many” relationship kind.
-
#many_to_many_custom_fields ⇒ Array
Return the names of all the many_to_many custom_fields of this object.
-
#non_relationship_custom_fields ⇒ Array
Return the rules of the custom fields which do not describe a relationship.
-
#relationship_custom_fields ⇒ Array
Return the rules of the custom fields which describe a relationship.
-
#select_custom_fields ⇒ Object
Return the names of all the select fields of this object.
Instance Method Details
#belongs_to_custom_fields ⇒ Array
Return the names of all the belongs_to custom_fields of this object
134 135 136 |
# File 'lib/custom_fields/target_helpers.rb', line 134 def belongs_to_custom_fields group_custom_fields 'belongs_to' end |
#custom_fields_basic_attributes ⇒ Hash
Build a hash for all the non-relationship fields meaning string, text, date, boolean, select, file types. This hash stores their name and their value.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/custom_fields/target_helpers.rb', line 55 def custom_fields_basic_attributes {}.tap do |hash| self.non_relationship_custom_fields.each do |rule| name, type = rule['name'], rule['type'].to_sym # method of the custom getter method_name = "#{type}_attribute_get" hash.merge!(self.class.send(method_name, self, name)) end end end |
#custom_fields_basic_attributes=(attributes) ⇒ Object
Set the values (and their related fields) for all the non-relationship fields meaning string, text, date, boolean, select, file types.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/custom_fields/target_helpers.rb', line 73 def custom_fields_basic_attributes=(attributes) self.non_relationship_custom_fields.each do |rule| name, type = rule['name'], rule['type'].to_sym # method of the custom getter method_name = "#{type}_attribute_set" self.class.send(method_name, self, name, attributes) end end |
#custom_fields_methods(&filter) ⇒ List
Return the list of the getters dynamically based on the custom_fields recipe in order to get the formatted values of the custom fields. If a block is passed, then the list will be filtered accordingly with the following logic. If the block is evaluated as true, then the method will be kept in the list, otherwise it will be removed.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/custom_fields/target_helpers.rb', line 20 def custom_fields_methods(&filter) self.custom_fields_recipe['rules'].map do |rule| method = self.custom_fields_getters_for rule['name'], rule['type'] if block_given? filter.call(rule) ? method : nil else method end end.compact.flatten end |
#custom_fields_safe_setters ⇒ List
List all the setters that are used by the custom_fields in order to get updated thru a html form for instance.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/custom_fields/target_helpers.rb', line 36 def custom_fields_safe_setters self.custom_fields_recipe['rules'].map do |rule| case rule['type'].to_sym when :date, :date_time, :money then "formatted_#{rule['name']}" when :file then [rule['name'], "remove_#{rule['name']}"] when :select, :belongs_to then ["#{rule['name']}_id", "position_in_#{rule['name']}"] when :has_many, :many_to_many then nil else rule['name'] end end.compact.flatten end |
#file_custom_fields ⇒ Array
Return the names of all the file custom_fields of this object
126 127 128 |
# File 'lib/custom_fields/target_helpers.rb', line 126 def file_custom_fields group_custom_fields 'file' end |
#has_many_custom_fields ⇒ Array
Return the names of all the has_many custom_fields of this object
142 143 144 |
# File 'lib/custom_fields/target_helpers.rb', line 142 def has_many_custom_fields group_custom_fields('has_many') { |rule| [rule['name'], rule['inverse_of']] } end |
#is_a_custom_field_many_relationship?(name) ⇒ Boolean
Check if the rule defined by the name is a “many” relationship kind. A “many” relationship includes “has_many” and “many_to_many”
91 92 93 94 95 |
# File 'lib/custom_fields/target_helpers.rb', line 91 def is_a_custom_field_many_relationship?(name) rule = self.custom_fields_recipe['rules'].detect do |rule| rule['name'] == name && _custom_field_many_relationship?(rule['type']) end end |
#many_to_many_custom_fields ⇒ Array
Return the names of all the many_to_many custom_fields of this object. It also adds the property used to set/get the target ids.
151 152 153 |
# File 'lib/custom_fields/target_helpers.rb', line 151 def many_to_many_custom_fields group_custom_fields('many_to_many') { |rule| [rule['name'], "#{rule['name'].singularize}_ids"] } end |
#non_relationship_custom_fields ⇒ Array
Return the rules of the custom fields which do not describe a relationship.
101 102 103 104 105 |
# File 'lib/custom_fields/target_helpers.rb', line 101 def non_relationship_custom_fields self.custom_fields_recipe['rules'].find_all do |rule| !%w(belongs_to has_many many_to_many).include?(rule['type']) end end |
#relationship_custom_fields ⇒ Array
Return the rules of the custom fields which describe a relationship.
111 112 113 114 115 |
# File 'lib/custom_fields/target_helpers.rb', line 111 def relationship_custom_fields self.custom_fields_recipe['rules'].find_all do |rule| %w(belongs_to has_many many_to_many).include?(rule['type']) end end |
#select_custom_fields ⇒ Object
Return the names of all the select fields of this object
118 119 120 |
# File 'lib/custom_fields/target_helpers.rb', line 118 def select_custom_fields group_custom_fields 'select' end |