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



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



300
301
302
303
304
# File 'lib/common_lib/action_view_extension/base.rb', line 300

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



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



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



294
295
296
297
298
# File 'lib/common_lib/action_view_extension/base.rb', line 294

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



306
307
308
309
310
# File 'lib/common_lib/action_view_extension/base.rb', line 306

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



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



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



270
271
272
273
274
# File 'lib/common_lib/action_view_extension/base.rb', line 270

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



276
277
278
279
280
# File 'lib/common_lib/action_view_extension/base.rb', line 276

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



288
289
290
291
292
# File 'lib/common_lib/action_view_extension/base.rb', line 288

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



282
283
284
285
286
# File 'lib/common_lib/action_view_extension/base.rb', line 282

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



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

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

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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://'
  #  production returns http:// even when it is clearly https:// ?
  #  double slash seems to make everyone happy
  protocol = '//'  #controller.try(:request).try(:protocol) || '//'
  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


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



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



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/common_lib/action_view_extension/base.rb', line 208

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


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



231
232
233
234
235
236
237
238
239
# File 'lib/common_lib/action_view_extension/base.rb', line 231

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



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;



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



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;



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



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, class: 'label' )
        else
          label( object_name, method, label_text, class: 'label' ) <<
          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



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

def nbsp
  "&nbsp;".html_safe
end

#padk(value = nil) ⇒ Object



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

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

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



265
266
267
# File 'lib/common_lib/action_view_extension/base.rb', line 265

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’



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



221
222
223
224
225
226
227
228
229
# File 'lib/common_lib/action_view_extension/base.rb', line 221

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



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



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

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

#ynodk(value = nil) ⇒ Object



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

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

#ynordk(value = nil) ⇒ Object



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

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

#ynrdk(value = nil) ⇒ Object



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

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