Class: ActiveRecord::Base

Inherits:
Object show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/active_record/base_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descObject



70
71
72
# File 'lib/active_record/base_patch.rb', line 70

def desc
  scoped(:order => "#{self.table_name}.created_at desc")
end

.each_with_transaction(ids) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/base_patch.rb', line 47

def each_with_transaction(ids)
  if ids.blank?
    return :empty
  end
  
  begin
    records = find(ids)
    transaction do
      records.each do |record|
        raise unless yield(record)
      end
    end
  rescue
    :error
  else
    :success
  end
end

.fuzzy(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/base_patch.rb', line 23

def fuzzy(args)
  or_empty_scope(args) do
    query_params = []
    query_string = []

    args.each {|model, attrs|
      table, field, data = model, *attrs.to_a.flatten
      table, field, data = [table.table_name, field.to_s, handle_input(data).join("|")]

      unless data.blank?
        query_string << "#{table}.#{field} regexp ?"
        query_params << data
      end
    }

    unless query_string.empty?
      query_string = query_string.join(" or ")
      scoped({:conditions => [query_string] + query_params})
    else
      scoped
    end
  end
end

.method_chains(params, *method_names) ⇒ Object



17
18
19
20
21
# File 'lib/active_record/base_patch.rb', line 17

def method_chains(params, *method_names)
  method_names.inject(scoped) do |result, method_name|
    result.send(method_name, params[method_name])
  end
end

.newestObject



66
67
68
# File 'lib/active_record/base_patch.rb', line 66

def newest
  scoped(:order => "#{self.table_name}.updated_at desc")
end

.regexp(field, data, table = table_name) ⇒ Object



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

def regexp(field, data, table = table_name)
  table_name = if table.is_a?(String)
    table
  elsif table < ActiveRecord::Base
    table.table_name
  else
    raise ArgumentError
  end

  regexp_condition(field, data, table_name)
end

Instance Method Details

#error_messagesObject



5
6
7
# File 'lib/active_record/base_patch.rb', line 5

def error_messages
  errors.full_messages.join("<br />")
end