Module: Invoicing::LineItem::ActMethods

Defined in:
lib/invoicing/line_item.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_line_item(*args) ⇒ Object

Declares that the current class is a model for line items (i.e. individual items on invoices and credit notes).

The name of any attribute or method required by LineItem (as documented on the LineItem module) may be used as an option, with the value being the name under which that particular method or attribute can be found. This allows you to use names other than the defaults. For example, if your database column storing the line item value is called net_price instead of net_amount:

acts_as_line_item :net_amount => :net_price


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/invoicing/line_item.rb', line 144

def acts_as_line_item(*args)
  Invoicing::ClassInfo.acts_as(Invoicing::LineItem, self, args)
  
  info = line_item_class_info
  if info.previous_info.nil? # Called for the first time?
    # Set the 'amount' columns to act as currency values
    acts_as_currency_value(info.method(:net_amount), info.method(:tax_amount))
    
    before_validation :calculate_tax_amount
    
    extend Invoicing::FindSubclasses
    
    # Dynamically created named scopes
    named_scope :in_effect, lambda{
      ledger_assoc_id = line_item_class_info.method(:ledger_item).to_sym
      ledger_refl = reflections[ledger_assoc_id]
      ledger_table = ledger_refl.table_name # not quoted_table_name because it'll be quoted again
      status_column = ledger_refl.klass.send(:ledger_item_class_info).method(:status)
      { :joins => ledger_assoc_id,
        :conditions => {"#{ledger_table}.#{status_column}" => ['closed', 'cleared'] } }
    }
    
    named_scope :sorted, lambda{|column|
      column = line_item_class_info.method(column).to_s
      if column_names.include?(column)
        {:order => "#{connection.quote_column_name(column)}, #{connection.quote_column_name(primary_key)}"}
      else
        {:order => connection.quote_column_name(primary_key)}
      end
    }
  end
end