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


113
114
115
116
117
118
# File 'lib/rails_admin/abstract_model.rb', line 113

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

Instance Method Details

#build_statement_for_dateObject (protected)


168
169
170
# File 'lib/rails_admin/abstract_model.rb', line 168

def build_statement_for_date
  range_filter(*get_filtering_duration)
end

#build_statement_for_datetime_or_timestampObject (protected)


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

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

#build_statement_for_integer_decimal_or_floatObject (protected)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rails_admin/abstract_model.rb', line 148

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)


144
145
146
# File 'lib/rails_admin/abstract_model.rb', line 144

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

#build_statement_for_type_genericObject (protected)


133
134
135
136
137
138
139
140
141
142
# File 'lib/rails_admin/abstract_model.rb', line 133

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)


129
130
131
# File 'lib/rails_admin/abstract_model.rb', line 129

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

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


183
184
185
# File 'lib/rails_admin/abstract_model.rb', line 183

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

#to_statementObject


120
121
122
123
124
125
# File 'lib/rails_admin/abstract_model.rb', line 120

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)


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

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