Module: Postable

Defined in:
app/models/postable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



43
44
45
# File 'app/models/postable.rb', line 43

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#formatted_posted_at(default_msg = "not yet posted", options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/postable.rb', line 14

def formatted_posted_at(default_msg = "not yet posted", options = {})
  return default_msg unless posted_at

  if options[:format]
    posted_at.to_time.localtime.strftime options[:format]
  elsif postable_type == :date
    posted_at.strftime "%-m/%-d/%Y"
  else
    posted_at.localtime.strftime "%-m/%-d/%Y at %-l:%M %P"
  end
end

#post_from(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/postable.rb', line 26

def post_from(params)
  if params[:post_now].present? && !posted?
    self.posted_at = Time.now
  elsif params[:post_in_hour].present? && !posted?
    self.posted_at = 1.hour.from_now
  elsif params[:posted].present? && params[:posted_at_date].present?
    time = "#{params[:posted_at_date]} #{params[:posted_at_hour]}:#{params[:posted_at_minute]} #{params[:posted_at_meridiem]}"
    time = DateTime.parse time
    time = Time.local time.year, time.month, time.day, time.hour, time.min
    self.posted_at = time
  elsif params[:posted].present?
    self.posted_at = 1.hour.from_now
  else
    self.posted_at = nil
  end
end

#postable_nowObject



10
11
12
# File 'app/models/postable.rb', line 10

def postable_now
  self.class.postable_now
end

#postable_typeObject



6
7
8
# File 'app/models/postable.rb', line 6

def postable_type
  self.class.postable_type
end

#posted?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'app/models/postable.rb', line 2

def posted?
  posted_at && posted_at <= postable_now
end