Class: Levene::Models::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations
Defined in:
lib/levene/models/base.rb

Direct Known Subclasses

Account, Campaign, CampaignMember, Contact, Lead, Opportunity

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.abstractObject Also known as: abstract?

Returns the value of attribute abstract.



60
61
62
# File 'lib/levene/models/base.rb', line 60

def abstract
  @abstract
end

.attr_namesObject

Returns the value of attribute attr_names.



63
64
65
# File 'lib/levene/models/base.rb', line 63

def attr_names
  @attr_names
end

.column_hashObject

Returns the value of attribute column_hash.



64
65
66
# File 'lib/levene/models/base.rb', line 64

def column_hash
  @column_hash
end

.columnsObject

Returns the value of attribute columns.



62
63
64
# File 'lib/levene/models/base.rb', line 62

def columns
  @columns
end

Class Method Details

.abstract!Object



66
67
68
# File 'lib/levene/models/base.rb', line 66

def abstract!
  self.abstract = true
end

.connection_established?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/levene/models/base.rb', line 124

def connection_established?
  !connection.nil? && connection.has_session?
end

.establish_connectionObject



128
129
130
131
# File 'lib/levene/models/base.rb', line 128

def establish_connection
  self.connection = Levene::Connection.new
  self.connection. ENV["SALESFORCE_DEV_USER"], ENV["SALESFORCE_DEV_PASS"]
end

.find(*ids) ⇒ Object



75
76
77
# File 'lib/levene/models/base.rb', line 75

def find(*ids)
  results = self.connection.find(self,ids)
end

.find_by_soql(query) ⇒ Object



79
80
81
82
83
# File 'lib/levene/models/base.rb', line 79

def find_by_soql(query)
  result = self.connection.binding.query(self, query)
  return [] if result == [nil]
  result.collect {|r| obj = self.new; obj.attributes = r; obj}
end

.get_column_definitionsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/levene/models/base.rb', line 85

def get_column_definitions
  self.columns = self.connection.binding.describe(self)
  self.columns.each do |col|
    next if col[:deprecatedAndHidden] == "true"

    attribute = (col[:name][0].downcase + col[:name][1..-1]).underscore
    self.attr_names ||= []
    self.attr_names << attribute.to_sym
    self.column_hash ||= {}
    self.column_hash[attribute.to_sym] = col

    attr_accessor attribute

    if col[:nillable] == "false" && col[:createable] == "true" && col[:defaultedOnCreate] != "true"
      validates_presence_of attribute
    end

    if col[:type] == "string" && col[:length] && col[:length].to_i > 0
      validates_length_of attribute, :maximum => col[:length].to_i
    end

    if col[:picklistValues]
      valid_values = col[:picklistValues].select {|h| h[:active] == "true"}.collect {|h| h[:value]}
      default = col[:picklistValues].find {|h| h[:defaultValue] == "true"}
      if col[:nillable] == "true" || col[:createable] == "false"
        validates_inclusion_of attribute, :in => valid_values, :allow_nil => true
      else
        validates_inclusion_of attribute, :in => valid_values
      end

      if default
        class_eval """def #{attribute}
            @#{attribute} ||= \"#{default[:value]}\"
           end"""
      end
    end
  end
end

.inherited(klazz) ⇒ Object



70
71
72
73
# File 'lib/levene/models/base.rb', line 70

def inherited(klazz)
  klazz.abstract = false
  klazz.get_column_definitions
end

Instance Method Details

#attributesObject



12
13
14
15
16
17
# File 'lib/levene/models/base.rb', line 12

def attributes
  self.class.attr_names.inject({}) do |memo, name|
    memo[name] = self.send(name)
    memo
  end
end

#attributes=(attrs) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/levene/models/base.rb', line 19

def attributes=(attrs)
  attrs.each do |attr, val|
    if attr[0] == attr[0].upcase
      attr = attr.to_s.underscore
      attr = attr[0].downcase + attr[1..-1]
    end

    if attr == "id" && Array === val
      val = val.first
    end

    self.send "#{attr}=", val if self.respond_to?("#{attr}=".to_sym)
  end
end

#saveObject



34
35
36
37
38
39
40
# File 'lib/levene/models/base.rb', line 34

def save
  if self.id.nil?
    self.connection.binding.create(self) 
  else
    self.connection.binding.update(self)
  end
end

#to_s_objectObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/levene/models/base.rb', line 42

def to_s_object
  attributeMap = self.attributes.inject({}) do |memo, attr|
    next memo if attr.last.nil? || attr.last.blank?
    levene_attr_name = attr.first.to_s.camelize
    if attr.first.to_s.ends_with?("__c")
      levene_attr_name.gsub!(/_c$/, "__c")
    end
    memo[levene_attr_name] = attr.last
    memo
  end

  fieldsToNull = self.attributes.select {|k,v| self.class.column_hash[k][:createable] == "true" && (v.nil? || v.blank?)}.collect {|k,v| k.to_s.camelize}
  fieldsToNull = fieldsToNull.collect {|k| k[0].downcase + k[1..-1]}
  
  {:sObject => {"xsi:type" => self.class.model_name.demodulize}.merge(attributeMap)}
end