Module: ChupakabraTools::ActiveRecord

Defined in:
lib/chupakabra_tools/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allowed_fields_to_sortObject



46
47
48
49
50
51
# File 'lib/chupakabra_tools/active_record.rb', line 46

def self.allowed_fields_to_sort
    unless defined?(@allowed_fields_to_sort)
        @allowed_fields_to_sort = []
    end
    @allowed_fields_to_sort
end

.allowed_fields_to_sort=(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chupakabra_tools/active_record.rb', line 53

def self.allowed_fields_to_sort= value
    @allowed_fields_to_sort = []
    if value
        if value.is_a?(Array)
            value.each do |i|
                @allowed_fields_to_sort.push i.to_s
            end
        else
            @allowed_fields_to_sort.push value.to_s
        end
    end
end

.default_sort_fieldObject



34
35
36
# File 'lib/chupakabra_tools/active_record.rb', line 34

def self.default_sort_field
    defined?(@default_sort_field) ? @default_sort_field : nil
end

.default_sort_field=(value) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/chupakabra_tools/active_record.rb', line 38

def self.default_sort_field= value
    if value
        @default_sort_field = value.to_s
    else
        @default_sort_field = nil
    end
end

.invert_order(sort_order = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/chupakabra_tools/active_record.rb', line 10

def self.invert_order(sort_order = nil)
    return 'desc' if sort_order.nil? || sort_order.empty?
    sort_order = sanitize_sortorder(sort_order)
    return 'desc' if sort_order.downcase == 'asc'
    'asc'
end

Instance Method Details

#sanitize_order(sort_order = nil) ⇒ Object



3
4
5
6
7
8
# File 'lib/chupakabra_tools/active_record.rb', line 3

def sanitize_order(sort_order = nil)
    return 'asc' if  sort_order.nil? || sort_order.empty?
    return 'asc' if sort_order.downcase == 'asc'
    return 'desc' if sort_order.downcase == 'desc'
    'asc'
end

#sanitize_sort_by(sort_by) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chupakabra_tools/active_record.rb', line 17

def sanitize_sort_by(sort_by)


    unless sort_by.nil? || sort_by.empty?

        fields_to_sort.each do |fts|
            if fts.downcase == sort_by
                return sort_by
            end
        end
        return default_sort_by
    end

    default_sort_by.nil? || default_sort_by.empty? ? default_sort_by : 'id'
end