Module: Days::Helpers

Defined in:
lib/days/helpers.rb

Instance Method Summary collapse

Instance Method Details

#configObject



5
6
7
# File 'lib/days/helpers.rb', line 5

def config
  Days::App.config
end

#csrf_tagObject



21
22
23
# File 'lib/days/helpers.rb', line 21

def csrf_tag
  Rack::Csrf.csrf_tag(env)
end

#csrf_tokenObject



17
18
19
# File 'lib/days/helpers.rb', line 17

def csrf_token
  Rack::Csrf.csrf_token(env)
end

#current_userObject



13
14
15
# File 'lib/days/helpers.rb', line 13

def current_user
  @current_user ||= session[:user_id] ? User.where(id: session[:user_id]).first : nil
end

#entry_path(entry, allow_draft = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/days/helpers.rb', line 25

def entry_path(entry, allow_draft=false)
  return nil unless allow_draft || entry.published?

  published_at = entry.published_at
  hash = {
    year: published_at.year.to_s.rjust(2, '0'),
    month: published_at.month.to_s.rjust(2, '0'),
    day: published_at.day.to_s.rjust(2, '0'),
    hour: published_at.hour.to_s.rjust(2, '0'),
    minute: published_at.min.to_s.rjust(2, '0'),
    second: published_at.sec.to_s.rjust(2, '0'),
    slug: entry.slug, id: entry.id
  }
  config.permalink.gsub(/{(\w+?)}/) { hash[$1.to_sym] }
end

#logged_in?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/days/helpers.rb', line 9

def logged_in?
  !!session[:user_id]
end

#lookup_entry(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/days/helpers.rb', line 41

def lookup_entry(path)
  regexp = Regexp.compile(Regexp.escape(config.permalink).gsub(/\\{(\w+?)\\}/) { "(?<#{$1}>.+?)" } + "$")
  m = regexp.match(path)
  return nil unless m

  match = m.names.inject({}) do |hash, k|
    hash[k.to_sym] = m[k]
    hash
  end


  if match[:id] || match[:slug]
    if match[:id]
      query = Entry.where(id: match[:id])
    else
      query = Entry.where(slug: match[:slug])
    end

    entry = query.first
    return nil unless entry
    published_at = entry.published_at
    return nil unless published_at

    return nil if match[:slug]   && match[:slug]        != entry.slug
    return nil if match[:id]     && match[:id].to_i     != entry.id
    return nil if match[:year]   && match[:year].to_i   != published_at.year
    return nil if match[:month]  && match[:month].to_i  != published_at.month
    return nil if match[:day]    && match[:day].to_i    != published_at.day
    return nil if match[:hour]   && match[:hour].to_i   != published_at.hour
    return nil if match[:minute] && match[:minute].to_i != published_at.min
    return nil if match[:second] && match[:second].to_i != published_at.sec

    return entry
  else
    match_time = {}.tap do |h|
      [:year, :month, :day, :hour, :minute, :second].each do |k|
        h[k] = match[k] && match[k].to_i
      end
    end
    range_begin = Time.local(
      match_time[:year], match_time[:month] || 1, match_time[:day] || 1,
      match_time[:hour] || 0, match_time[:minute] || 0, match_time[:second] || 0
    )
    range_end = Time.local(
      match_time[:year], match_time[:month] || 12, match_time[:day] || 31,
      match_time[:hour] || 23, match_time[:minute] || 59, match_time[:second] || 59
    )

    query = Entry.where(published_at: (range_begin .. range_end))

    if query.count.zero?
      return nil
    elsif query.count == 1
      return query.first
    else
      return query
    end
  end
end