Module: SugarCRM::AttributeMethods::ClassMethods

Defined in:
lib/sugarcrm/attributes/attribute_methods.rb

Instance Method Summary collapse

Instance Method Details

#attributes_from_moduleObject

Returns a hash of the module fields from the module



5
6
7
8
9
10
11
# File 'lib/sugarcrm/attributes/attribute_methods.rb', line 5

def attributes_from_module
  fields = {}.with_indifferent_access
  self._module.fields.keys.sort.each do |k|
    fields[k] = nil
  end
  fields
end

#flatten_conditions_for(condition) ⇒ Object

Takes a condition like: [:zip, [“> 75000”, “< 80000”]] and flattens it to: [“accounts.zip > 75000”, “accounts.zip < 80000”]



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sugarcrm/attributes/attribute_methods.rb', line 22

def flatten_conditions_for(condition)
  conditions = []
  attribute, attribute_conditions = condition
  # Make sure we wrap the attribute condition in an array for EZ handling...
  Array.wrap(attribute_conditions).each do |attribute_condition| 
    # parse operator in cases where: 
    #   :attribute => '>= some_value', 
    #   :attribute => "LIKE '%value%'", 
    # fallback to '=' operator as default]
    operator = attribute_condition.to_s[/^([!<>=]*(LIKE|IS|NOT|\s)*)(.*)$/,1].strip!
    # Default to = if we can't resolve the condition.
    operator ||= '=' 
    # Extract value from query
    value = $3
    unless attribute_condition.class == FalseClass
      if attribute_condition.class == TrueClass
        # fix value for checkboxes: users can pass true as condition, should be converted to '1' (false case for checkboxes is treated separately below)
        value = (attribute_condition.class == TrueClass ? '1' : '0')
      end
      
      # TODO: Write a test for sending invalid attribute names.  
      # strip single quotes
      value = value.strip[/'?([^']*)'?/,1] 
      conditions << "#{table_name_for(attribute)}.#{attribute} #{operator} \'#{value}\'"
    else
      # When a user creates a custom checkbox field, a column is added to the *_cstm table for that module (e.g. contacts_cstm for Contacts module).
      # Each time a new record is created, the value of the checkbox will be stored in that _cstm table.
      # However, records that exsited before that field was created are absent from the _cstm table.
      # To return the expected results when a user is searching for records with an unchecked checkbox, we must return all records that aren't present in
      # the _cstm table with a value of 1 (returning the record with 0 in the table will ignore the pre-existing records).
      # Here, we create the appropriate query that will return all records that don't have a value of "true" in the checkbox field.
      conditions << "#{self._module.table_name}.id NOT IN (SELECT id_c FROM #{table_name_for(attribute)} WHERE #{table_name_for(attribute)}.#{attribute} #{operator} 1)"
    end
  end
  conditions
end

#table_name_for(attribute) ⇒ Object

Returns the table name for a given attribute



13
14
15
16
17
18
19
# File 'lib/sugarcrm/attributes/attribute_methods.rb', line 13

def table_name_for(attribute)
  table_name = self._module.table_name
  if attribute.to_s =~ /_c$/
    table_name = self._module.custom_table_name
  end
  table_name
end