Module: HomesteadingHelpers::ApplicationHelper

Defined in:
app/helpers/homesteading_helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#autofocus_valueObject



110
111
112
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 110

def autofocus_value
  !editing?
end

#canonical_url(post = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 39

def canonical_url(post=nil)
  if post.nil?
    "http://#{setting :long_domain}"
  else
    "http://#{setting :long_domain}/#{post.path}"
  end
end

#editing?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 89

def editing?
  action_name == "edit"
end

#external_silo_url_key(name) ⇒ Object



195
196
197
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 195

def external_silo_url_key(name)
  "#{name.downcase}_url".to_sym
end

#external_silosObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 180

def external_silos
  %w[
    Facebook
    Foursquare
    Instagram
    Medium
    Tumblr
    Twitter
    Vimeo
    Vine
    WordPress
    YouTube
  ]
end

#human_readable_date(datetime) ⇒ Object



93
94
95
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 93

def human_readable_date(datetime)
  datetime.strftime("%F")
end

#human_readable_time(datetime) ⇒ Object



97
98
99
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 97

def human_readable_time(datetime)
  datetime.strftime("%l:%M%p").downcase
end

#in_reply_to_urls(post) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 138

def in_reply_to_urls(post)
  html = []

  post.in_reply_to.split(" ").each do |in_reply_to|
    text = in_reply_to.gsub(/^https?:\/\//, "").gsub(/\//, "/<wbr />").html_safe
    url  = in_reply_to

    unless url.match(/^http/)
      url = "http://#{url}"
    end

    rel = show_action? ? "in-reply-to external" : "external"
    html << link_to(text, url, class: "u-in-reply-to h-cite", rel: rel).html_safe
  end

  html.join(" ").html_safe
end

#index_action?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 75

def index_action?
  action_name == "index"
end

#is_a_reply?(post) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 199

def is_a_reply?(post)
  !post.in_reply_to.blank?
end

#license_textObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 212

def license_text
  output = []

  # license
  license = ::License.find(setting(:license))

  if license.name == "All Rights Reserved"
    # Default
    output << "#{license.name} #{license.short_code}"
  else
    # Creative Commons and Public Domain (CC0)
    output << link_to("#{license.name} (#{license.short_code})", license.url, rel: "license")
  end


  # range of years
  years_range = Time.now.year

  first_post      = Post.first
  first_post_year = first_post.nil? ? years_range : first_post.published_at.year

  if first_post_year == years_range
    output << years_range
  else
    output << "#{first_post_year}&ndash;#{years_range}"
  end


  # author's name
  output << link_to(setting(:author_name), setting(:author_url), class: "p-author h-card")

  output.join(" ").html_safe
end


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 156

def link_to_syndication(post)
  html = (:p, "Also posted to")

  links = []

  external_silos.each do |external|
    if post.respond_to?(external_silo_url_key(external)) &&
       post.send(external_silo_url_key(external)) &&
       !post.send(external_silo_url_key(external)).blank?

       rel = "external "
       rel << "syndication" if show_action?

      links << (:li, link_to(external, post.send(external_silo_url_key(external)), rel: rel, class:  "u-syndication"))
    end
  end

  if links.blank?
    space
  else
    html << (:ul, links.flatten.join.html_safe)
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 105

def logged_in?
  # true
  false
end

#nth_of_day(post) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 25

def nth_of_day(post)
  nth = Post.where(
          year:  post.year,
          month: post.month,
          day:   post.day
        ).all.sort_by{|n| n.published_at}.index(post) + 1

  if nth.nil?
    nth = 1
  end

  nth.to_sxg
end

#page_description(post = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 59

def page_description(post=nil)
  if index_action?
    page_description = "#{setting(:post_type).pluralize.capitalize} by #{setting :author_name}"
  elsif show_action?
    page_description = post.content
  else
    page_description = setting(:site_description)
  end

  page_description
end

#post_typeObject



114
115
116
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 114

def post_type
  setting(:post_type)
end


47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 47

def rel_canonical_link_tag(post)
  url = "http://#{setting :long_domain}"

  if index_action?
    url = "http://#{setting :long_domain}/#{setting(:post_type).pluralize.downcase}"
  elsif show_action?
    url = canonical_url(post)
  end

  tag(:link, id: "canonical", rel: "canonical", type: "text/html", href: url)
end

#setting(key) ⇒ Object



134
135
136
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 134

def setting(key)
  ::Setting.where(key: key).first.content
end

#short_url(post) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 16

def short_url(post)
  pieces =  [setting(:post_short_code)]

  pieces << Date.parse("#{post.year}-#{post.month}-#{post.day}").to_sxg
  pieces << nth_of_day(post)

  "http://#{setting :short_domain}/#{pieces.join}"
end


4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 4

def shorturl_link_tag(post)
  if index_action?
    url = "http://#{setting :short_domain}/#{setting(:post_short_code)}"
  elsif show_action?
    url = short_url(post)
  else
    url = "http://#{setting :short_domain}"
  end

  tag(:link, id: "shortlink", rel: "shortlink", type: "text/html", href: url)
end

#show_action?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 79

def show_action?
  action_name == "show"
end

#site_titleObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 118

def site_title
  title = ""

  if setting(:site_title).blank?
    title << post_type.capitalize.pluralize
  else
    title << setting(:site_title)
  end

  if @page_title
    title << " - #{@page_title}"
  end

  title.html_safe
end

#site_urlObject

TODO: do this without #eval ?



204
205
206
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 204

def site_url
  eval "#{setting(:post_type).pluralize}_url"
end

#small_word_tag(word) ⇒ Object



101
102
103
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 101

def small_word_tag(word)
  (:b, word, class: "small-word small-word-#{word}")
end

#spaceObject



71
72
73
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 71

def space
  " "
end

#tag_url(tag) ⇒ Object



208
209
210
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 208

def tag_url(tag)
  site_url + "/tags/" + tag.gsub(/ /, "+")
end

#write_action?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'app/helpers/homesteading_helpers/application_helper.rb', line 83

def write_action?
  unless controller_name == "settings"
    action_name =~ /new|edit/
  end
end