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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 107

def condition_for_date_bridge_type(column, value, like_pattern)
  operator = ActiveScaffold::Finder::NumericComparators.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)
  else
    unless operator.nil?
      ["#{column.search_sql} #{value[:opt]} ?", from_value.to_s(:db)] unless from_value.nil?
    else
      ["#{column.search_sql} BETWEEN ? AND ?", from_value.to_s(:db), to_value.to_s(:db)] unless from_value.nil? && to_value.nil?  
    end
  end
end

#date_bridge_column_date?(column) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 189

def date_bridge_column_date?(column)
  if [: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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 122

def date_bridge_from_to(column, value)
  conversion = column.column[:type] == :date ? :to_date : :to_time
  case value[:opt]
  when 'RANGE'
    date_bridge_from_to_for_range(column, value).collect(&conversion)
  when 'PAST', 'FUTURE'
    date_bridge_from_to_for_trend(column, value).collect(&conversion)
  else
    ['from', 'to'].collect { |field| condition_value_for_datetime(value[field], conversion)}
  end
end

#date_bridge_from_to_for_range(column, value) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 165

def date_bridge_from_to_for_range(column, value)
  case value[:range]
  when 'TODAY'
    return date_bridge_now.beginning_of_day, date_bridge_now.end_of_day
  when 'YESTERDAY'
    return date_bridge_now.ago(1.day).beginning_of_day, date_bridge_now.ago(1.day).end_of_day
  when 'TOMORROW'
    return 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 ['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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 138

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
    return 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
    return from, to
  end
end

#date_bridge_nowObject



134
135
136
# File 'lib/active_scaffold/bridges/shared/date_bridge.rb', line 134

def date_bridge_now
  Time.zone.now
end