Class: Incline::DataTablesRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/incline/data_tables_request.rb

Overview

Parses the parameters sent by a request from datatables.

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ DataTablesRequest

Initializes the data tables request parameters.

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
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
136
137
138
139
140
141
142
# File 'lib/incline/data_tables_request.rb', line 54

def initialize(params = {}, &block)
  raise ArgumentError, 'A block is required to return the starting ActiveRecord scope.' unless block_given?

  @config                   = {}
  @config[:starting_scope]  = block

  params = params.deep_symbolize_keys

  force_regex = params[:force_regex]

  if params[:draw]
    @config[:draw]   = params[:draw].to_s.to_i
    @config[:start]  = params[:start].to_s.to_i
    @config[:length] = params[:length].to_s.to_i
    @config[:locate_id] = params[:locate_id]


    tmp = params[:search]
    if tmp && !tmp[:value].blank?
      if tmp[:regex].to_bool || force_regex
        @config[:search] =
            if tmp[:value].is_a?(::Regexp)
              tmp[:value]
            else
              begin
                Regexp.new(tmp[:value].to_s, Regexp::IGNORECASE)
              rescue RegexpError
                tmp[:value].to_s
              end
            end
      elsif tmp
        @config[:search] = tmp[:value].to_s
      end
    else
      @config[:search] = nil
    end

    tmp               = params[:columns]
    @config[:columns] = [ ]
    if tmp
      tmp = tmp.each_with_index.to_a.map{|(a,b)| [b.to_s,a]}.to_h.deep_symbolize_keys if tmp.is_a?(::Array)
      tmp.each do |id, col|
        col[:id] = id.to_s.to_i
        col[:name] = col[:data] if col[:name].blank?
        col[:searchable] = col[:searchable].to_bool
        col[:orderable] = col[:orderable].to_bool

        if col[:search] && !col[:search][:value].blank?
          if col[:search][:regex].to_bool || force_regex
            col[:search] =
                if col[:search][:value].is_a?(::Regexp)
                  col[:search][:value]
                else
                  begin
                    Regexp.new(col[:search][:value].to_s, Regexp::IGNORECASE)
                  rescue RegexpError
                    col[:search][:value].to_s
                  end
                end
          else
            col[:search] = col[:search][:value].to_s
          end
        else
          col[:search] = nil
        end

        @config[:columns] << col
      end
    end
    @config[:columns].freeze

    tmp             = params[:order]
    @config[:order] = { }
    if tmp
      tmp = tmp.each_with_index.to_a.map{|(a,b)| [b.to_s,a]}.to_h.deep_symbolize_keys if tmp.is_a?(::Array)
      tmp.each do |_, order|
        col_id = order[:column].to_i
        col = columns.find{|c| c[:id] == col_id}
        if col
          @config[:order][col[:name]] = ((order[:dir] || 'asc').downcase).to_sym
        end
      end
    end
    @config[:order].freeze
  else
    @config[:draw] = :not_provided
  end

end

Instance Method Details

#columnsObject

The columns requested.



34
35
36
# File 'lib/incline/data_tables_request.rb', line 34

def columns
  @config[:columns]
end

#drawObject

Draw counter.



8
9
10
# File 'lib/incline/data_tables_request.rb', line 8

def draw
  @config[:draw]
end

#errorObject

The error message, if any?



190
191
192
193
# File 'lib/incline/data_tables_request.rb', line 190

def error
  records
  @config[:error]
end

#error?Boolean

Is there an error to display?

Returns:

  • (Boolean)


197
198
199
# File 'lib/incline/data_tables_request.rb', line 197

def error?
  !error.blank?
end

#lengthObject

Number of records to return.

Can be any positive value, or -1 to indicate that all records should be returned.



22
23
24
# File 'lib/incline/data_tables_request.rb', line 22

def length
  @config[:length]
end

#locate_idObject

If we are searching for a specific ID, this is the ID to locate.



48
49
50
# File 'lib/incline/data_tables_request.rb', line 48

def locate_id
  @config[:locate_id]
end

#orderingObject

The row ordering.



40
41
42
# File 'lib/incline/data_tables_request.rb', line 40

def ordering
  @config[:order]
end

#provided?Boolean

Where the data tables parameters provided?

Returns:

  • (Boolean)


146
147
148
# File 'lib/incline/data_tables_request.rb', line 146

def provided?
  draw != :not_provided
end

#record_location(refresh = false) ⇒ Object

If #locate_id is non-zero, this is the record number for the specified ID using our filtering parameters.

This will be an absolute zero-based record number for the ID. It can be used to figure out the page that the record would appear on.



162
163
164
165
# File 'lib/incline/data_tables_request.rb', line 162

def record_location(refresh = false)
  @config[:record_location] = nil if refresh
  @config[:record_location] ||= find_record
end

#records(refresh = false) ⇒ Object

Gets the records returned by this request.



169
170
171
172
# File 'lib/incline/data_tables_request.rb', line 169

def records(refresh = false)
  @config[:records] = nil if refresh
  @config[:records] ||= load_records
end

#records_filteredObject

Gets the total number of records after filtering.



183
184
185
186
# File 'lib/incline/data_tables_request.rb', line 183

def records_filtered
  records
  @config[:records_filtered]
end

#records_totalObject

Gets the total number of records before filtering.



176
177
178
179
# File 'lib/incline/data_tables_request.rb', line 176

def records_total
  records
  @config[:records_total]
end

#refresh!Object

Refreshes the data and returns the request instance.



152
153
154
155
# File 'lib/incline/data_tables_request.rb', line 152

def refresh!
  records true
  self
end

#searchObject

Text or regular expression to search with.



28
29
30
# File 'lib/incline/data_tables_request.rb', line 28

def search
  @config[:search]
end

#startObject

First record to return.



14
15
16
# File 'lib/incline/data_tables_request.rb', line 14

def start
  @config[:start]
end