Module: ActiveScaffold::Bridges::Shared::DateBridge::Finder::ClassMethods

Defined in:
lib/active_scaffold/bridges/shared/date_bridge.rb

Instance Method Summary collapse

Instance Method Details

#condition_for_date_bridge_type(column, value, like_pattern) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 118

def condition_for_date_bridge_type(column, value, like_pattern)
  operator = ActiveScaffold::Finder::NUMERIC_COMPARATORS.include?(value['opt']) && value['opt'] != 'BETWEEN' ? value['opt'] : nil
  from_value, to_value = date_bridge_from_to(column, value)

  if column.search_sql.is_a? Proc
    column.search_sql.call(from_value, to_value, operator)
  elsif operator.nil?
    ['%<search_sql>s BETWEEN ? AND ?', from_value, to_value] unless from_value.nil? || to_value.nil?
  else
    ["%<search_sql>s #{value['opt']} ?", from_value] unless from_value.nil?
  end
end

#date_bridge_column_date?(column) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 204

def date_bridge_column_date?(column)
  if %i[date_picker datetime_picker].include? column.form_ui
    column.form_ui == :date_picker
  else
    (!column.column.nil? && [:date].include?(column.column.type))
  end
end

#date_bridge_from_to(column, value) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 131

def date_bridge_from_to(column, value)
  conversion = datetime_conversion_for_condition(column)
  case value['opt']
  when 'RANGE'
    values = date_bridge_from_to_for_range(column, value)
    # Avoid calling to_time, not needed and broken on rails >= 4, because return local time instead of UTC
    values.collect!(&conversion) if conversion != :to_time
    values
  when 'PAST', 'FUTURE'
    values = date_bridge_from_to_for_trend(column, value)
    # Avoid calling to_time, not needed and broken on rails >= 4, because return local time instead of UTC
    values.collect!(&conversion) if conversion != :to_time
    values
  else
    %w[from to].collect { |field| condition_value_for_datetime(column, value[field], conversion) }
  end
end

#date_bridge_from_to_for_range(column, value) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 180

def date_bridge_from_to_for_range(column, value)
  case value['range']
  when 'TODAY'
    [date_bridge_now.beginning_of_day, date_bridge_now.end_of_day]
  when 'YESTERDAY'
    [date_bridge_now.ago(1.day).beginning_of_day, date_bridge_now.ago(1.day).end_of_day]
  when 'TOMORROW'
    [date_bridge_now.in(1.day).beginning_of_day, date_bridge_now.in(1.day).end_of_day]
  else
    range_type, range = value['range'].downcase.split('_')
    raise ArgumentError unless %w[week month year].include?(range)
    case range_type
    when 'this'
      return date_bridge_now.send("beginning_of_#{range}".to_sym), date_bridge_now.send("end_of_#{range}")
    when 'prev'
      return date_bridge_now.ago(1.send(range.to_sym)).send("beginning_of_#{range}".to_sym), date_bridge_now.ago(1.send(range.to_sym)).send("end_of_#{range}".to_sym)
    when 'next'
      return date_bridge_now.in(1.send(range.to_sym)).send("beginning_of_#{range}".to_sym), date_bridge_now.in(1.send(range.to_sym)).send("end_of_#{range}".to_sym)
    else
      return nil, nil
    end
  end
end

#date_bridge_from_to_for_trend(column, value) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 153

def date_bridge_from_to_for_trend(column, value)
  case value['opt']
  when 'PAST'
    trend_number = [value['number'].to_i, 1].max
    now = date_bridge_now
    if date_bridge_column_date?(column)
      from = now.beginning_of_day.ago(trend_number.send(value['unit'].downcase.singularize.to_sym))
      to = now.end_of_day
    else
      from = now.ago(trend_number.send(value['unit'].downcase.singularize.to_sym))
      to = now
    end
    [from, to]
  when 'FUTURE'
    trend_number = [value['number'].to_i, 1].max
    now = date_bridge_now
    if date_bridge_column_date?(column)
      from = now.beginning_of_day
      to = now.end_of_day.in(trend_number.send(value['unit'].downcase.singularize.to_sym))
    else
      from = now
      to = now.in(trend_number.send(value['unit'].downcase.singularize.to_sym))
    end
    [from, to]
  end
end

#date_bridge_nowObject



149
150
151
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 149

def date_bridge_now
  Time.zone.now
end