Class: RailsAdmin::AbstractModel::StatementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/abstract_model.rb

Defined Under Namespace

Classes: FilteringDuration

Instance Method Summary collapse

Constructor Details

#initialize(column, type, value, operator) ⇒ StatementBuilder

Returns a new instance of StatementBuilder.


118
119
120
121
122
123
# File 'lib/rails_admin/abstract_model.rb', line 118

def initialize(column, type, value, operator)
  @column = column
  @type = type
  @value = value
  @operator = operator
end

Instance Method Details

#build_statement_for_dateObject (protected)


172
173
174
175
176
177
# File 'lib/rails_admin/abstract_model.rb', line 172

def build_statement_for_date
  start_date, end_date = get_filtering_duration
  start_date = (start_date.to_date rescue nil) if start_date
  end_date = (end_date.to_date rescue nil) if end_date
  range_filter(start_date, end_date)
end

#build_statement_for_datetime_or_timestampObject (protected)


179
180
181
182
183
184
# File 'lib/rails_admin/abstract_model.rb', line 179

def build_statement_for_datetime_or_timestamp
  start_date, end_date = get_filtering_duration
  start_date = start_date.try(:beginning_of_day) if start_date
  end_date = end_date.try(:end_of_day) if end_date
  range_filter(start_date, end_date)
end

#build_statement_for_integer_decimal_or_floatObject (protected)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rails_admin/abstract_model.rb', line 152

def build_statement_for_integer_decimal_or_float
  case @value
  when Array then
    val, range_begin, range_end = *@value.collect do |v|
      next unless v.to_i.to_s == v || v.to_f.to_s == v
      @type == :integer ? v.to_i : v.to_f
    end
    case @operator
    when 'between'
      range_filter(range_begin, range_end)
    else
      column_for_value(val) if val
    end
  else
    if @value.to_i.to_s == @value || @value.to_f.to_s == @value
      @type == :integer ? column_for_value(@value.to_i) : column_for_value(@value.to_f)
    end
  end
end

#build_statement_for_typeObject (protected)


148
149
150
# File 'lib/rails_admin/abstract_model.rb', line 148

def build_statement_for_type
  raise('You must override build_statement_for_type in your StatementBuilder')
end

#build_statement_for_type_genericObject (protected)


137
138
139
140
141
142
143
144
145
146
# File 'lib/rails_admin/abstract_model.rb', line 137

def build_statement_for_type_generic
  build_statement_for_type || begin
    case @type
    when :date
      build_statement_for_date
    when :datetime, :timestamp
      build_statement_for_datetime_or_timestamp
    end
  end
end

#get_filtering_durationObject (protected)


133
134
135
# File 'lib/rails_admin/abstract_model.rb', line 133

def get_filtering_duration
  FilteringDuration.new(@operator, @value).get_duration
end

#range_filter(_min, _max) ⇒ Object (protected)


190
191
192
# File 'lib/rails_admin/abstract_model.rb', line 190

def range_filter(_min, _max)
  raise('You must override range_filter in your StatementBuilder')
end

#to_statementObject


125
126
127
128
129
# File 'lib/rails_admin/abstract_model.rb', line 125

def to_statement
  return if [@operator, @value].any? { |v| v == '_discard' }
  unary_operators[@operator] || unary_operators[@value] ||
    build_statement_for_type_generic
end

#unary_operatorsObject (protected)


186
187
188
# File 'lib/rails_admin/abstract_model.rb', line 186

def unary_operators
  raise('You must override unary_operators in your StatementBuilder')
end