Module: Droom::DroomHelper

Defined in:
app/helpers/droom/droom_helper.rb

Instance Method Summary collapse

Instance Method Details

#action_menu(thing, locals = {}) ⇒ Object



22
23
24
25
26
27
28
29
# File 'app/helpers/droom/droom_helper.rb', line 22

def action_menu(thing, locals={})
  if can?(:edit, thing)
    type = thing.class.to_s.downcase.underscore
    classname = type.split('/').last
    locals[classname.to_sym] = thing
    render :partial => "#{type.pluralize}/action_menu", :locals => locals
  end
end


10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/droom/droom_helper.rb', line 10

def action_menulink(thing, html_options={})
  if can?(:edit, thing)
    classname = thing.class.to_s.downcase.underscore.split('/').last
    html_options.reverse_merge!({
      :class => "",
      :data => {:menu => "#{classname}_#{thing.id}"}
    })
    html_options[:class] << " menu"
    link_to t(:edit), "#", html_options if editable?(thing)
  end
end

#admin?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/helpers/droom/droom_helper.rb', line 43

def admin?
  current_user && current_user.admin?
end

#allowed?(permission_code) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'app/helpers/droom/droom_helper.rb', line 6

def allowed?(permission_code)
  current_user.admin? || current_user.permitted?(permission_code)
end

#cloud(these, threshold = 0, biggest = 3.0, smallest = 1.3) ⇒ Object

This will apply cloud-weighting to any list of items. They must have a ‘weight’ attribute and be ready to accept a ‘cloud_size’ attribute.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/helpers/droom/droom_helper.rb', line 130

def cloud(these, threshold=0, biggest=3.0, smallest=1.3)
  counts = these.map{|t| t.weight.to_i}.compact
  if counts.any?
    max = counts.max
    min = counts.min
    if max == min
      these.each do |this|
        this.cloud_size = sprintf("%.2f", biggest/2 + smallest/2)
      end
    else
      steepness = Math.log(max - (min-1))/(biggest - smallest)
      these.each do |this|
        offset = Math.log(this.weight.to_i - (min-1))/steepness
        this.cloud_size = sprintf("%.2f", smallest + offset)
      end
    end
    if block_given?
      these.each do |this|
        yield this
      end
    end
  end
end

#day_namesObject



162
163
164
# File 'app/helpers/droom/droom_helper.rb', line 162

def day_names
  ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
end

#deletable?(thing) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/helpers/droom/droom_helper.rb', line 93

def deletable?(thing)
  admin? || current_user == thing.created_by
end

#dropbox?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/helpers/droom/droom_helper.rb', line 67

def dropbox?
  current_user and !!current_user.dropbox_token
end

#dropbox_auth_urlObject



75
76
77
78
79
80
81
82
83
# File 'app/helpers/droom/droom_helper.rb', line 75

def dropbox_auth_url
  dbs = dropbox_session
  # get an auth link address, with our register action as the callback
  authorization_url = dbs.get_authorize_url(droom.register_dropbox_tokens_url)
  # store the requesting dropbox session, serialized, in our user's session cookie
  # if the oauth confirmation is successful, we will need it.
  session[:dropbox_session] = dbs.serialize
  authorization_url
end


31
32
33
34
35
# File 'app/helpers/droom/droom_helper.rb', line 31

def dropbox_link(folder)
  if dropbox? && current_user.pref('dropbox.strategy') == 'clicked' && folder.populated? && !folder.dropboxed_for?(current_user)
    link_to t(:copy_to_dropbox), droom.dropbox_folder_url(folder), :id => "dropbox_folder_#{folder.id}", :class => 'dropboxer minimal', :data => {:action => "remove", :removed => "#dropbox_folder_#{folder.id}"}
  end
end

#dropbox_sessionObject



37
38
39
40
41
# File 'app/helpers/droom/droom_helper.rb', line 37

def dropbox_session
  # note that we usually don't want to pick up an existing dropbox session. That happens in the dropbox_tokens_controller, when
  # following up an access token round trip, but in the view any existing session has probably expired and we're better off with a new one.
  DropboxSession.new(Droom.dropbox_app_key, Droom.dropbox_app_secret)
end

#editable?(thing) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/helpers/droom/droom_helper.rb', line 89

def editable?(thing)
  admin? || current_user == thing.created_by
end

#month_header_for(date) ⇒ Object



103
104
105
# File 'app/helpers/droom/droom_helper.rb', line 103

def month_header_for(date)
  ('h3', l(date, :format => :month_header))
end


97
98
99
100
101
# File 'app/helpers/droom/droom_helper.rb', line 97

def nav_link_to(name, url, options={})
  options[:class] ||= ""
  options[:class] << "here" if (request.path == url) || (url != "/" && request.path =~ /^#{url}/)
  link_to name, url, options
end

#pageclassObject



47
48
49
# File 'app/helpers/droom/droom_helper.rb', line 47

def pageclass
  controller.controller_name
end

#pagination_summary(collection, options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/droom/droom_helper.rb', line 107

def pagination_summary(collection, options = {})
  entry_name = options[:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
  summary = if collection.num_pages < 2
    case collection.total_count
    when 0; "No #{entry_name.pluralize} found"
    when 1; "Displaying <strong>1</strong> #{entry_name}:"
    else;   "Displaying <strong>all #{collection.total_count}</strong> #{entry_name.pluralize}:"
    end
  else
    offset = (collection.current_page - 1) * collection.limit_value
    %{Displaying <strong>%d&nbsp;-&nbsp;%d</strong> of <strong>%d</strong> #{entry_name.pluralize}: } % [
      offset + 1,
      offset + collection.length,
      collection.total_count
    ]
  end
  summary.html_safe
end

#preference_checkbox(key, options = {}) ⇒ Object



51
52
53
# File 'app/helpers/droom/droom_helper.rb', line 51

def preference_checkbox(key, options={})
  render :partial => "droom/preferences/checkbox", :locals => options.merge({:key => key})
end

#preference_radio_set(key, *values) ⇒ Object



55
56
57
# File 'app/helpers/droom/droom_helper.rb', line 55

def preference_radio_set(key, *values)
  render :partial => "droom/preferences/radio_set", :locals => {:key => key, :values => values}
end

#shorten(text, length = 64, separator = " ") ⇒ Object



59
60
61
62
63
64
65
# File 'app/helpers/droom/droom_helper.rb', line 59

def shorten(text, length=64, separator=" ")
  text = strip_tags(text)
  length = length[:length] if length.is_a?(Hash)
   :span, class: 'shortened', title: text do
    truncate(text, {:length => length, :separator => separator})
  end
end

#show_dropbox_links?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/helpers/droom/droom_helper.rb', line 71

def show_dropbox_links?
  dropbox?# && preference is 'clicked'
end

#url_for_date(date) ⇒ Object



158
159
160
# File 'app/helpers/droom/droom_helper.rb', line 158

def url_for_date(date)
  droom.events_url(:year => date.year, :month => date.month, :mday => date.day)
end

#url_for_month(date) ⇒ Object



154
155
156
# File 'app/helpers/droom/droom_helper.rb', line 154

def url_for_month(date)
  droom.events_url(:year => date.year, :month => date.month)
end

#visible?(thing) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/helpers/droom/droom_helper.rb', line 85

def visible?(thing)
  admin? || privileged? || current_user.can_see?(thing)
end