Module: JennyHelper

Defined in:
lib/jenny/jenny_helper.rb

Instance Method Summary collapse

Instance Method Details

#app_action_name(action, options, element_name = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/jenny/jenny_helper.rb', line 9

def app_action_name action, options, element_name=nil
  a_name = []
  a_name << options[:prepend_id].to_sym if options.has_key? :prepend_id
  a_name << action
  a_name << element_name unless element_name.nil?
  a_name = a_name.join('_')
end

#app_checkbox(f, name) ⇒ Object



91
92
93
94
95
96
# File 'lib/jenny/jenny_helper.rb', line 91

def app_checkbox f,name
  html = ''
  html << f.label(name, t(".#{name}"))
  html << f.check_box(name)
  wrap '.check_box', html
end

#app_error(f, name, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jenny/jenny_helper.rb', line 136

def app_error f, name, options={}
  include_error = options.delete :include_error
  any_errors    = f.object.errors[name.to_sym].any?
  error_txt     = options.delete :error_txt
  return unless any_errors
  txt = t(".#{name}")
  txt = error_txt if error_txt
  errors = f.object.errors[name.to_sym]
  errors.map!{|e|"#{txt} #{e}"}
  msg    = errors.join '<br />'
  wrap ".err_msg.#{name}", msg
end

#app_expand_select(f, name, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/jenny/jenny_helper.rb', line 120

def app_expand_select f, name, options={}
  opts = eval "#{f.object.class.to_s.tableize.singularize}_#{name}_select_options"
  html = ''
  html << f.label(name, t(".#{name}"))
  html << wrap('.options', opts.collect{|o|wrap('.option', o[0], :value => o[1])}.join.html_safe)
  html << f.hidden_field(name)
  wrap ".expand_select.#{name}", html
end

#app_form(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jenny/jenny_helper.rb', line 45

def app_form *args
  options,id_args,target,action,args = shes_just_a_memory *args
  partial_path = []
  partial_path << options[:namespace] if options.has_key? :namespace
  partial_path << target.class.to_s.tableize
  partial_path = partial_path.join('/')
  locals = {}
  args.each{ |a| locals[a.class.to_s.tableize.singularize.to_sym] = a }
  locals.merge! options[:locals]  if options[:locals]
  partial "#{partial_path}/form", locals
end

#app_form_for(*args, &proc) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jenny/jenny_helper.rb', line 17

def app_form_for *args, &proc
  raise ArgumentError, "Missing block" unless block_given?
  options,id_args,target,action,args = shes_just_a_memory(*args)
  options[:style] ||= 'display: none'
  singular_name   = target.class.to_s.underscore
  action_name     = app_action_name action, options, 'form'
  form_path       = options[:url] || app_formatted_path(args,options)

  class_name      = ['app_form',"#{singular_name}_form","#{action}_#{singular_name}_form"]
  class_name      << options[:class] if options[:class]
  class_name      = class_name.join ' '

  loading_args    = id_args.clone
  loading_args    << (options[:after] ? { after: options[:after] } : {} )

  html = {
    class: class_name,
    style: options[:style]
  }

  html.merge! options[:attrs] if options[:attrs]
  form_for target,
    url:    form_path,
    remote: true,
    html:   html,
    &proc
end

#app_formatted_path(args, options) ⇒ Object



2
3
4
5
6
7
# File 'lib/jenny/jenny_helper.rb', line 2

def app_formatted_path args, options
  namespace = []
  namespace << options[:namespace].to_sym if options.has_key? :namespace
  path = namespace + args
  path
end

#app_hidden_field(f, *fields) ⇒ Object



216
217
218
# File 'lib/jenny/jenny_helper.rb', line 216

def app_hidden_field f, *fields
  fields.collect { |field| f.hidden_field(field) }.join.html_safe
end

#app_label(name, options = {}) ⇒ Object



129
130
131
132
133
134
# File 'lib/jenny/jenny_helper.rb', line 129

def app_label name, options={}
  include_label = options.delete :include_label
  label_txt     = options.delete :label_txt
  label_txt     = label_txt ? label_txt : t(".#{name}")
  label_tag name, label_txt unless include_label
end

#app_password_field(f, name, *args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jenny/jenny_helper.rb', line 176

def app_password_field f,name,*args
  label_txt  = args[0].is_a?(String)
  options    = label_txt ? {label_txt: args[0]} : args.extract_options!
  any_errors = f.object.errors[name.to_sym].any?
  err_class  = '.err' if any_errors

  label_html = app_label        name, options
  input_html = f.password_field name, options
  error_html = app_error     f, name, options

  html = ''
  html << label_html.to_s
  html << input_html.to_s
  html << error_html.to_s
  html << capture(&block) if block_given?
  html = wrap ".password_field.#{name}#{err_class}", html
end

#app_select(f, name, *args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jenny/jenny_helper.rb', line 98

def app_select f, name, *args
  label_txt  = args[0].is_a?(String)
  options    = label_txt ? {label_txt: args[0]} : args.extract_options!
  any_errors = f.object.errors[name.to_sym].any?
  err_class  = '.err' if any_errors

  table_name     = f.object.class.to_s.tableize.singularize
  select_options = eval "#{table_name}_#{name}_select_options"

  label_html  = app_label    name, options
  select_html = f.select     name, select_options, include_blank: '----'
  error_html  = app_error f, name, options

  html = ''
  html << label_html.to_s
  html << select_html.to_s
  html << error_html.to_s
  html << capture(&block) if block_given?
  html << wrap('.clear')
  html = wrap ".select.#{name}#{err_class}", html
end

#app_text_area(f, name) ⇒ Object



209
210
211
212
213
214
# File 'lib/jenny/jenny_helper.rb', line 209

def app_text_area f, name
  html = ''
  html << f.label(name, t(".#{name}"))
  html << f.text_area(name)
  wrap ".text_area.#{name}", html
end

#app_text_field(f, name, *args, &block) ⇒ Object

params: form_object, name, options form_option, name, label_txt

options:

include_label (default: true) set false to not render label html
include_txt change the label's default text
include_error (default: true) set false to not render error html


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/jenny/jenny_helper.rb', line 157

def app_text_field f,name, *args, &block
  label_txt           = args.shift if args[0].is_a?(String)
  options             = args.extract_options!
  options[:label_txt] = label_txt if label_txt
  any_errors          = f.object.errors[name.to_sym].any?
  err_class           = '.err' if any_errors

  label_html = app_label   name, options
  input_html = f.text_field name, options
  error_html = app_error f, name, options

  html = ''
  html << label_html.to_s
  html << input_html.to_s
  html << error_html.to_s
  html << capture(&block) if block_given?
  html = wrap ".text_field.#{name}#{err_class}", html
end

#app_text_field_tag(name, *args) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/jenny/jenny_helper.rb', line 194

def app_text_field_tag name, *args
  label_txt  = args[0].is_a?(String)
  options    = label_txt ? {label_txt: args[0]} : args.extract_options!
  value    ||= ''
  value      = options.delete :value if options[:value]

  label_html = app_label      name, options
  input_html = text_field_tag name, value

  html = ''
  html << label_html.to_s
  html << input_html.to_s
  html = wrap ".text_field.#{name}", html
end

#locals(helper_binding, *locals) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jenny/jenny_helper.rb', line 79

def locals helper_binding, *locals
  options = locals.extract_options!
  result = {}
  vars = eval "local_variables", helper_binding
  for var in vars
    next if var == 'html'
    result[var.to_sym] = eval "#{var}", helper_binding
  end
  result.merge! options
  result.symbolize_keys
end

#partial(*args) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/jenny/jenny_helper.rb', line 269

def partial *args
  options = args.extract_options!
  render_options = {}

  wrap_me = (args.length >= 2 && [Symbol,String,Array].include?(args[0].class) && args[1].is_a?(String))

  empty = options.delete(:empty)

  if wrap_me
    html_options          = (options.key?(:html) ? options[:html] : {})
    html_options[:style]  = options.delete :style
    html_options[:top]    = options.delete :top
    html_options[:bottom] = options.delete :bottom
    html_options[:before] = options.delete :before
    html_options[:after]  = options.delete :after

    selector = args.delete_at(0)
  else
    before = options.delete :before
    after  = options.delete :after
  end

  render_options[:partial] = args[0]
  render_options[:locals] = options
  if args.length >= 2
    render_options[:collection] = args[1]
    render_options[:as]         = options.delete :as if options[:as]
  end

  render_html = if render_options.key?(:collection) && render_options[:collection].empty?
    render_options.delete :collection
    empty ? empty : ''
  else
    render render_options
  end

  html = if wrap_me
    wrap selector, render_html, html_options
  else
    html = ''
    html << before if before
    html << (render_html.nil? ? '' : render_html)
    html << after if after
    html
  end
  html.html_safe
end

#shes_just_a_memory(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jenny/jenny_helper.rb', line 65

def shes_just_a_memory(*args)
  #And she use to mean so much to me...
  options = args.extract_options!
  id_args = args.dup
  id_args << { :namespace => options[:namespace] } if options.has_key? :namespace
  target = args.last
  action = if options[:method] == :delete
    'delete'
  else
    target.new_record? ? 'new' : 'edit'
  end
  [options,id_args,target,action,args]
end

#show_form_errors(target, form_id) ⇒ Object



58
59
60
61
62
63
# File 'lib/jenny/jenny_helper.rb', line 58

def show_form_errors target, form_id
  page << "Jenny.remove_form_errors('#{form_id}')"
  target.errors.each do |field,message|
    page << "Jenny.show_form_errors('#{form_id}','#{field}','#{message}')"
  end
end

#wrap(selector, *args) ⇒ Object



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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/jenny/jenny_helper.rb', line 220

def wrap selector, *args
  options = args.extract_options!

  render_html = args.shift
  selector = [selector] unless selector.is_a?(Array)

  top    = options.delete :top
  bottom = options.delete :bottom
  before = options.delete :before
  after  = options.delete :after

  wrap_render_html = ''
  wrap_render_html << top if top
  wrap_render_html << (render_html.nil? ? '' : render_html)
  wrap_render_html << bottom if bottom
  html = wrap_render_html
  front = ''
  back = []
  for s in selector
    class_name = []
    tag = 'div'
    id = ''
    s = ".#{s}##{s}" if s.is_a?(Symbol)
    s.scan(/^%\w+|\G[\.|\#]\w+/).each do |ss|
      name = ss.reverse.chop.reverse
      case ss[0,1]
        when '.'; class_name << name
        when '#'; id  = name
        when '%'; tag = name
      end
    end
    attributes = []
    extra_class_names = options.delete :class
    extra_class_names = " #{extra_class_names}"
    options.each{|k,v|attributes << "#{k}='#{v}'"}
    c = class_name.empty? ? extra_class_names : class_name.join(' ')+extra_class_names
    c = "class='#{c}'"
    i = id.blank? ? '' : "id='#{id}'"
    front << "<#{tag} #{c} #{i} #{attributes.join(' ')}>"
    back << "</#{tag}>"
  end
  render_html = front+html+back.reverse.join('')
  html = ''
  html << before if before
  html << render_html
  html << after if after
  html.html_safe
end