Module: ArelExtensions::Math

Included in:
Attributes
Defined in:
lib/arel_extensions/math.rb

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

function + between String and others (convert in string) allows you to concatenate 2 or more strings together. Date and integer adds or subtracts a specified time interval from a date.



17
18
19
20
21
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
58
59
60
61
62
63
# File 'lib/arel_extensions/math.rb', line 17

def +(other)
  case self
    when Arel::Nodes::Quoted
      return self.concat(other)
    when Arel::Nodes::Grouping
      if self.expr.left.is_a?(String) || self.expr.right.is_a?(String)
        return self.concat(other)
      else
        return Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
      end
    when  ArelExtensions::Nodes::Function,ArelExtensions::Nodes::Case
      return case self.return_type
    when :string, :text
      self.concat(other)
    when :integer, :decimal, :float, :number, :int
      Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
    when :date, :datetime
      ArelExtensions::Nodes::DateAdd.new [self, other]
    else
      self.concat(other)
    end
  when  Arel::Nodes::Function
    Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
  else
    begin
      col = Arel::Table.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s]
    rescue Exception
      col = nil
    end
    if (!col) #if the column doesn't exist in the database
      Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new(self, other))
    else
      arg = col.type
      if arg == :integer || (!arg)
        other = other.to_i if other.is_a?(String)
        Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
      elsif arg == :decimal || arg == :float
        other = Arel.sql(other) if other.is_a?(String) # Arel should accept Float & BigDecimal!
        Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
      elsif arg == :datetime || arg == :date
        ArelExtensions::Nodes::DateAdd.new [self, other]
      elsif arg == :string || arg == :text
        self.concat(other)
      end
    end
  end
end

#-(other) ⇒ Object

function returns the time between two dates function returns the substraction between two ints



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/arel_extensions/math.rb', line 67

def -(other)
  case self
  when Arel::Nodes::Grouping
    if self.expr.left.is_a?(Date) || self.expr.left.is_a?(DateTime)
      Arel::Nodes::Grouping.new(ArelExtensions::Nodes::DateSub.new [self, other])
    else
      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
    end
  when ArelExtensions::Nodes::Function, ArelExtensions::Nodes::Case
    case self.return_type
    when :string, :text # ???
      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other)) # ??
    when :integer, :decimal, :float, :number
      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
    when :date, :datetime
      ArelExtensions::Nodes::DateSub.new [self, other]
    else
      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
    end
  when Arel::Nodes::Function
    Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
  else
    begin
      col = Arel::Table.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s]
    rescue Exception
      col = nil
    end
    if (!col) #if the column doesn't exist in the database
      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
    else
      arg = col.type
      if (arg == :date || arg == :datetime)
        case other
        when Arel::Attributes::Attribute
          begin
            col2 = Arel::Table.engine.connection.schema_cache.columns_hash(other.relation.table_name)[other.name.to_s]
          rescue Exception
            col2 = nil
          end
          if (!col2) #if the column doesn't exist in the database
            ArelExtensions::Nodes::DateSub.new [self, other]
          else
            arg2 = col2.type
            if arg2 == :date || arg2 == :datetime
              ArelExtensions::Nodes::DateDiff.new [self, other]
            else
              ArelExtensions::Nodes::DateSub.new [self, other]
            end
          end
        when Arel::Nodes::Node, DateTime, Time, String, Date
          ArelExtensions::Nodes::DateDiff.new [self, other]
        when  ArelExtensions::Nodes::Duration, Integer
          ArelExtensions::Nodes::DateSub.new [self, other]
        else # ActiveSupport::Duration
          ArelExtensions::Nodes::DateAdd.new [self, -other]
        end
      else
        case other
        when Integer, Float, BigDecimal
          Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, Arel.sql(other.to_s)))
        when String
          Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, Arel.sql(other)))
        else
          Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
        end
      end
    end
  end
end