Module: CustomFields::TargetHelpers

Defined in:
lib/custom_fields/target_helpers.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_custom_fieldsArray

Return the names of all the belongs_to custom_fields of this object

Returns:

  • (Array)

    List of names



136
137
138
# File 'lib/custom_fields/target_helpers.rb', line 136

def belongs_to_custom_fields
  group_custom_fields 'belongs_to'
end

#custom_fields_basic_attributesHash

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.

Returns:

  • (Hash)

    Field name / formatted value



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/custom_fields/target_helpers.rb', line 55

def custom_fields_basic_attributes
  {}.tap do |hash|
    non_relationship_custom_fields.each do |rule|
      name = rule['name']
      type = 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.

Parameters:

  • The (Hash)

    attributes for the custom fields and their related fields.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/custom_fields/target_helpers.rb', line 74

def custom_fields_basic_attributes=(attributes)
  non_relationship_custom_fields.each do |rule|
    name = rule['name']
    type = 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.

Examples:

# keep all the methods except for the field named 'foo'
project.custom_fields_methods do |rule|
  rule['name] != 'foo'
end

Returns:

  • (List)

    a list of method names (string)



20
21
22
23
24
25
26
27
28
29
# File 'lib/custom_fields/target_helpers.rb', line 20

def custom_fields_methods(&filter)
  custom_fields_recipe['rules'].map do |rule|
    method = 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_settersList

List all the setters that are used by the custom_fields in order to get updated thru a html form for instance.

Returns:

  • (List)

    a list of method names (string)



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
  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']}", "remote_#{rule['name']}_url"]
    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_fieldsArray

Return the names of all the file custom_fields of this object

Returns:

  • (Array)

    List of names



128
129
130
# File 'lib/custom_fields/target_helpers.rb', line 128

def file_custom_fields
  group_custom_fields 'file'
end

#has_many_custom_fieldsArray

Return the names of all the has_many custom_fields of this object

Returns:

  • (Array)

    Array of array [name, inverse_of]



144
145
146
# File 'lib/custom_fields/target_helpers.rb', line 144

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”

Parameters:

  • name (String)

    The name of the rule

Returns:

  • (Boolean)

    True if the rule is a “many” relationship kind.



93
94
95
96
97
# File 'lib/custom_fields/target_helpers.rb', line 93

def is_a_custom_field_many_relationship?(name)
  rule = custom_fields_recipe['rules'].detect do |rule|
    rule['name'] == name && _custom_field_many_relationship?(rule['type'])
  end
end

#many_to_many_custom_fieldsArray

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.

Returns:

  • (Array)

    Array of array [name, <name in singular>_ids]



153
154
155
# File 'lib/custom_fields/target_helpers.rb', line 153

def many_to_many_custom_fields
  group_custom_fields('many_to_many') { |rule| [rule['name'], "#{rule['name'].singularize}_ids"] }
end

#non_relationship_custom_fieldsArray

Return the rules of the custom fields which do not describe a relationship.

Returns:

  • (Array)

    List of rules (Hash)



103
104
105
106
107
# File 'lib/custom_fields/target_helpers.rb', line 103

def non_relationship_custom_fields
  custom_fields_recipe['rules'].find_all do |rule|
    !%w[belongs_to has_many many_to_many].include?(rule['type'])
  end
end

#relationship_custom_fieldsArray

Return the rules of the custom fields which describe a relationship.

Returns:

  • (Array)

    List of rules (Hash)



113
114
115
116
117
# File 'lib/custom_fields/target_helpers.rb', line 113

def relationship_custom_fields
  custom_fields_recipe['rules'].find_all do |rule|
    %w[belongs_to has_many many_to_many].include?(rule['type'])
  end
end

#select_custom_fieldsObject

Return the names of all the select fields of this object



120
121
122
# File 'lib/custom_fields/target_helpers.rb', line 120

def select_custom_fields
  group_custom_fields 'select'
end