Module: Fortnight::ClassMethods

Defined in:
lib/fortnight.rb

Instance Method Summary collapse

Instance Method Details

#in_fortnight(year, month, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fortnight.rb', line 10

def in_fortnight(year, month, options={})

  begin_of_month = Time.new(year, month)
  half_month = begin_of_month.advance(days: 14).end_of_day
  final_of_month = begin_of_month.end_of_month.end_of_day

  start = options[:half] == :second ? half_month : begin_of_month
  finish = options[:half] == :second ? final_of_month : half_month

  field = options[:field] || 'created_at'

  scope = where("#{field} >= :s AND #{field} <= :f", s: start, f: finish)
  scope = scope.order(options[:order]) if options[:order]

  scope
end

#last_fortnightObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/fortnight.rb', line 27

def last_fortnight
  today = Date.today
  date = today - 15.days

  if today.day >= 1 && today.day <= 15
    self.in_fortnight(date.year, date.month, half: :second)
  else
    self.in_fortnight(date.year, date.month)
  end
end