Module: CommonLib::ActionViewExtension::Base

Defined in:
lib/common_lib/action_view_extension/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

[View source]

3
4
5
6
7
8
# File 'lib/common_lib/action_view_extension/base.rb', line 3

def self.included(base)
  base.class_eval do
    # This NEEDS to be HERE or gets stuck at method_missing_without_wrapping call????
    alias_method_chain( :method_missing, :wrapping )
  end
end

Instance Method Details

#_wrapped_adna_spans(object_name, method, options = {}) ⇒ Object

[View source]

297
298
299
300
301
# File 'lib/common_lib/action_view_extension/base.rb', line 297

def _wrapped_adna_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (ADNA[object.send(method)]||' ') ) )
end

#_wrapped_date_spans(object_name, method, options = {}) ⇒ Object

[View source]

104
105
106
107
108
# File 'lib/common_lib/action_view_extension/base.rb', line 104

def _wrapped_date_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => mdy(object.send(method)) ) )
end

#_wrapped_datetime_spans(object_name, method, options = {}) ⇒ Object

[View source]

110
111
112
113
114
# File 'lib/common_lib/action_view_extension/base.rb', line 110

def _wrapped_datetime_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => mdyhm(object.send(method)) ) )
end

#_wrapped_padk_spans(object_name, method, options = {}) ⇒ Object

[View source]

291
292
293
294
295
# File 'lib/common_lib/action_view_extension/base.rb', line 291

def _wrapped_padk_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (PADK[object.send(method)]||' ') ) )
end

#_wrapped_pos_neg_spans(object_name, method, options = {}) ⇒ Object

[View source]

303
304
305
306
307
# File 'lib/common_lib/action_view_extension/base.rb', line 303

def _wrapped_pos_neg_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (POSNEG[object.send(method)]||' ') ) )
end

#_wrapped_spans(object_name, method, options = {}) ⇒ Object

This is NOT a form field

[View source]

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
100
101
102
# File 'lib/common_lib/action_view_extension/base.rb', line 57

def _wrapped_spans(object_name,method,options={})
    object = instance_variable_get("@#{object_name}")
#   s =  "<span class='label'>#{options[:label_text]||method}</span>\n"

# could also "object.class.human_attribute_name method" ?

#   s =  "<span class='label'>#{options[:label_text]||I18n.translate(
#     "#{object.class.to_s.underscore}.#{method}",
#     :scope => "activerecord.attributes",
#     :default => method.to_s)}</span>\n" # if method is a symbol, tries to translate it too.




#
#   label = if options[:label_text].present?
#     options[:label_text]
#   else
#     l = ( I18n.translate("#{object.class.to_s.underscore}.#{method}",
#       :scope => "activerecord.attributes", :raise => true ) rescue false ) ||
#       method.to_s
#     l.send( :humanize or :titleize or :gsub('_', ' ') or ......
#   end
#
# titleize - capitalizes all words
# humanize - capitalizes only the first word ( same as other rails helpers, but I don't like )
# 
#

  s =  "<span class='label'>#{options[:label_text] ||
    ( I18n.translate("#{object.class.to_s.underscore}.#{method}",
      :scope => "activerecord.attributes", :raise => true ) rescue false ) ||
    method.to_s.humanize }</span>\n"  #  if method is a symbol, tries to translate it too.

  value = if options[:value]
    options[:value]
  else
# moved to top
#     object = instance_variable_get("@#{object_name}")
    value = object.send(method)
    value = (value.to_s.blank?)?'&nbsp;':value
  end
  s << "<span class='value'>#{value}</span>"
  s << (( options.has_key?(:post_text) ) ? "<span class='post_text'>#{options[:post_text]}</span>" : "")
  s.html_safe
end

#_wrapped_yes_or_no_spans(object_name, method, options = {}) ⇒ Object

This is NOT a form field

[View source]

117
118
119
120
121
122
123
124
# File 'lib/common_lib/action_view_extension/base.rb', line 117

def _wrapped_yes_or_no_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (object.send("#{method}?"))?'Yes':'No') )
#   s =  "<span class='label'>#{options[:label_text]||method}</span>\n"
#   value = (object.send("#{method}?"))?'Yes':'No'
#   s << "<span class='value'>#{value}</span>"
end

#_wrapped_yndk_spans(object_name, method, options = {}) ⇒ Object

[View source]

267
268
269
270
271
# File 'lib/common_lib/action_view_extension/base.rb', line 267

def _wrapped_yndk_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (YNDK[object.send(method)]||'&nbsp;') ) )
end

#_wrapped_ynodk_spans(object_name, method, options = {}) ⇒ Object

[View source]

273
274
275
276
277
# File 'lib/common_lib/action_view_extension/base.rb', line 273

def _wrapped_ynodk_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (YNODK[object.send(method)]||'&nbsp;') ) )
end

#_wrapped_ynordk_spans(object_name, method, options = {}) ⇒ Object

[View source]

285
286
287
288
289
# File 'lib/common_lib/action_view_extension/base.rb', line 285

def _wrapped_ynordk_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (YNORDK[object.send(method)]||'&nbsp;') ) )
end

#_wrapped_ynrdk_spans(object_name, method, options = {}) ⇒ Object

[View source]

279
280
281
282
283
# File 'lib/common_lib/action_view_extension/base.rb', line 279

def _wrapped_ynrdk_spans(object_name,method,options={})
  object = instance_variable_get("@#{object_name}")
  _wrapped_spans(object_name,method,options.update(
    :value => (YNRDK[object.send(method)]||'&nbsp;') ) )
end

#adna(value = nil) ⇒ Object

[View source]

258
259
260
# File 'lib/common_lib/action_view_extension/base.rb', line 258

def adna(value=nil)
  (ADNA[value]||'&nbsp;').html_safe
end

#aws_image_tag(image, options = {}) ⇒ Object

[View source]

192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/common_lib/action_view_extension/base.rb', line 192

def aws_image_tag(image,options={})
  #  in console @controller is nil
  #  rails 4 this seems to be controller not @controller now
  protocol = controller.try(:request).try(:protocol) || 'http://'
  host = 's3.amazonaws.com/'
#   bucket = ( defined?(RAILS_APP_NAME) && RAILS_APP_NAME ) || 'ccls'
  bucket = ( defined?(RAILS_APP_NAME) && RAILS_APP_NAME ) || 
    Rails.application.class.parent.to_s.downcase || 'ccls'
  src = "#{protocol}#{host}#{bucket}/images/#{image}"
  alt = options.delete(:alt) || options.delete('alt') || image
  tag('img',options.merge({:src => src, :alt => alt}))
end
[View source]

185
186
187
188
189
190
# File 'lib/common_lib/action_view_extension/base.rb', line 185

def destroy_link_to( title, url, options={}, &block )
  s = form_link_to( title, url, options.merge(
    'method' => 'delete',
    :class => 'destroy_link_to'
  ),&block )
end

#field_wrapper(method, options = {}, &block) ⇒ Object

[View source]

46
47
48
49
50
51
52
# File 'lib/common_lib/action_view_extension/base.rb', line 46

def field_wrapper(method,options={},&block)
  classes = [method,options[:class]].compact.join(' ')
  s =  "<div class='#{classes} field_wrapper'>\n"
  s << yield 
  s << "\n</div><!-- class='#{classes}' -->"
  s.html_safe
end

#flasherObject

[View source]

205
206
207
208
209
210
211
212
213
214
215
# File 'lib/common_lib/action_view_extension/base.rb', line 205

def flasher
  s = ''
  flash.each do |key, msg|
    s << ( :p, msg, :id => key, :class => "flash #{key}" )
    s << "\n"
  end
  s << "<noscript><p id='noscript' class='flash'>\n"
  s << "Javascript is required for this site to be fully functional.\n"
  s << "</p></noscript>\n"
  s.html_safe
end
[View source]

171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/common_lib/action_view_extension/base.rb', line 171

def form_link_to( title, url, options={}, &block )
  extra_tags = extra_tags_for_form(options)
  s =  "\n" <<
    "<form " <<
    "class='#{options.delete(:class)||'form_link_to'}' " <<
    "action='#{url_for(url)}' " <<
    "method='#{options.delete('method')}'>\n" <<
    extra_tags << "\n"
  s << (( block_given? )? capture(&block) : '')
  s << submit_tag(title, :name => nil ) << "\n" <<
    "</form>\n"
  s.html_safe
end

#javascripts(*args) ⇒ Object

[View source]

228
229
230
231
232
233
234
235
236
# File 'lib/common_lib/action_view_extension/base.rb', line 228

def javascripts(*args)
  @javascripts ||= []
  args.each do |javascript|
    unless @javascripts.include?(javascript.to_s)
      @javascripts.push(javascript.to_s)
      content_for(:head,javascript_include_tag(javascript, :type => 'text/javascript').to_s)
    end
  end
end

#mdy(date) ⇒ Object

[View source]

21
22
23
# File 'lib/common_lib/action_view_extension/base.rb', line 21

def mdy(date)
  ( date.blank? or !date.respond_to?(:strftime) ) ? nbsp : date.strftime("%m/%d/%Y")
end

#mdy_or_nil(date) ⇒ Object

For use in CSV output as don’t want the &nbsp;

[View source]

26
27
28
# File 'lib/common_lib/action_view_extension/base.rb', line 26

def mdy_or_nil(date)
  ( date.blank? or !date.respond_to?(:strftime) ) ? nil : date.strftime("%m/%d/%Y")
end

#mdyhm(datetime) ⇒ Object

[View source]

30
31
32
33
# File 'lib/common_lib/action_view_extension/base.rb', line 30

def mdyhm(datetime)
  ( datetime.blank? or !datetime.respond_to?(:strftime) ) ? nbsp : 
      datetime.strftime("%m/%d/%Y %H:%M (%Z)")
end

#mdyhm_or_nil(datetime) ⇒ Object

For use in CSV output as don’t want the &nbsp;

[View source]

36
37
38
39
# File 'lib/common_lib/action_view_extension/base.rb', line 36

def mdyhm_or_nil(datetime)
  ( datetime.blank? or !datetime.respond_to?(:strftime) ) ? nil : 
      datetime.strftime("%m/%d/%Y %H:%M (%Z)")
end

#method_missing_with_wrapping(symb, *args, &block) ⇒ Object

[View source]

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/common_lib/action_view_extension/base.rb', line 126

def method_missing_with_wrapping(symb,*args, &block)
  method_name = symb.to_s
  if method_name =~ /^wrapped_(.+)$/
    unwrapped_method_name = $1
#
# It'd be nice to be able to genericize all of the
# wrapped_* methods since they are all basically
# the same.
#   Strip of the "wrapped_"
#   Label
#   Call "unwrapped" method
#

    object_name = args[0]
    method      = args[1]

    content = field_wrapper(method,:class => unwrapped_method_name) do
      s = if respond_to?(unwrapped_method_name)
        options    = args.detect{|i| i.is_a?(Hash) }
        label_text = options.delete(:label_text) unless options.nil?
        if unwrapped_method_name == 'check_box'
          send("#{unwrapped_method_name}",*args,&block) <<
          label( object_name, method, label_text )
        else
          label( object_name, method, label_text ) <<
          send("#{unwrapped_method_name}",*args,&block)
        end
      else
        send("_#{method_name}",*args,&block)
      end

      s << (( block_given? )? capture(&block) : '')
#       send("_#{method_name}",*args) << 
#         (( block_given? )? capture(&block) : '')
    end
#     if block_called_from_erb?(block)
#       concat(content)
#     else
      content
#     end
  else
    method_missing_without_wrapping(symb,*args, &block)
  end
end

#nbspObject

[View source]

17
18
19
# File 'lib/common_lib/action_view_extension/base.rb', line 17

def nbsp
  "&nbsp;".html_safe
end

#padk(value = nil) ⇒ Object

[View source]

254
255
256
# File 'lib/common_lib/action_view_extension/base.rb', line 254

def padk(value=nil)
  (PADK[value]||'&nbsp;').html_safe
end

#pos_neg(value = nil) ⇒ Object Also known as: posneg

[View source]

262
263
264
# File 'lib/common_lib/action_view_extension/base.rb', line 262

def pos_neg(value=nil)
  (POSNEG[value]||'&nbsp;').html_safe
end

#required(text) ⇒ Object Also known as: req

Just a simple method to wrap the passed text in a span with class=‘required’

[View source]

12
13
14
# File 'lib/common_lib/action_view_extension/base.rb', line 12

def required(text)
  "<span class='required'>#{text}</span>".html_safe
end

#stylesheets(*args) ⇒ Object

Created to stop multiple entries of same stylesheet

[View source]

218
219
220
221
222
223
224
225
226
# File 'lib/common_lib/action_view_extension/base.rb', line 218

def stylesheets(*args)
  @stylesheets ||= []
  args.each do |stylesheet|
    unless @stylesheets.include?(stylesheet.to_s)
      @stylesheets.push(stylesheet.to_s)
      content_for(:head,stylesheet_link_tag(stylesheet.to_s))
    end
  end
end

#time_mdy(time) ⇒ Object

[View source]

41
42
43
44
# File 'lib/common_lib/action_view_extension/base.rb', line 41

def time_mdy(time)
  ( time.blank? or !time.respond_to?(:strftime) ) ? nbsp : 
      time.strftime("%I:%M %p %m/%d/%Y")
end

#yndk(value = nil) ⇒ Object

[View source]

238
239
240
# File 'lib/common_lib/action_view_extension/base.rb', line 238

def yndk(value=nil)
  (YNDK[value]||'&nbsp;').html_safe
end

#ynodk(value = nil) ⇒ Object

[View source]

242
243
244
# File 'lib/common_lib/action_view_extension/base.rb', line 242

def ynodk(value=nil)
  (YNODK[value]||'&nbsp;').html_safe
end

#ynordk(value = nil) ⇒ Object

[View source]

250
251
252
# File 'lib/common_lib/action_view_extension/base.rb', line 250

def ynordk(value=nil)
  (YNORDK[value]||'&nbsp;').html_safe
end

#ynrdk(value = nil) ⇒ Object

[View source]

246
247
248
# File 'lib/common_lib/action_view_extension/base.rb', line 246

def ynrdk(value=nil)
  (YNRDK[value]||'&nbsp;').html_safe
end