Module: JennyHelper
- Defined in:
- lib/jenny/jenny_helper.rb
Instance Method Summary collapse
- #app_action_name(action, options, element_name = nil) ⇒ Object
- #app_checkbox(f, name) ⇒ Object
- #app_error(f, name, options = {}) ⇒ Object
- #app_expand_select(f, name, options = {}) ⇒ Object
- #app_form(*args) ⇒ Object
- #app_form_for(*args, &proc) ⇒ Object
- #app_formatted_path(args, options) ⇒ Object
- #app_hidden_field(f, *fields) ⇒ Object
- #app_label(name, options = {}) ⇒ Object
- #app_password_field(f, name, *args) ⇒ Object
- #app_select(f, name, *args) ⇒ Object
- #app_text_area(f, name) ⇒ Object
-
#app_text_field(f, name, *args, &block) ⇒ Object
params: form_object, name, options form_option, name, label_txt.
- #app_text_field_tag(name, *args) ⇒ Object
- #locals(helper_binding, *locals) ⇒ Object
- #partial(*args) ⇒ Object
- #shes_just_a_memory(*args) ⇒ Object
- #show_form_errors(target, form_id) ⇒ Object
- #wrap(selector, *args) ⇒ Object
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, , element_name=nil a_name = [] a_name << [:prepend_id].to_sym if .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, ={} include_error = .delete :include_error any_errors = f.object.errors[name.to_sym].any? error_txt = .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 f, name, ={} 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 ,id_args,target,action,args = shes_just_a_memory *args partial_path = [] partial_path << [:namespace] if .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! [:locals] if [:locals] partial "#{partial_path}/form", locals end |
#app_form_for(*args, &proc) ⇒ Object
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? ,id_args,target,action,args = shes_just_a_memory(*args) [:style] ||= 'display: none' singular_name = target.class.to_s.underscore action_name = app_action_name action, , 'form' form_path = [:url] || app_formatted_path(args,) class_name = ['app_form',"#{singular_name}_form","#{action}_#{singular_name}_form"] class_name << [:class] if [:class] class_name = class_name.join ' ' loading_args = id_args.clone loading_args << ([:after] ? { after: [:after] } : {} ) html = { class: class_name, style: [:style] } html.merge! [:attrs] if [: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, namespace = [] namespace << [:namespace].to_sym if .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, ={} include_label = .delete :include_label label_txt = .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) = label_txt ? {label_txt: args[0]} : args. any_errors = f.object.errors[name.to_sym].any? err_class = '.err' if any_errors label_html = app_label name, input_html = f.password_field name, error_html = app_error f, name, 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) = label_txt ? {label_txt: args[0]} : args. any_errors = f.object.errors[name.to_sym].any? err_class = '.err' if any_errors table_name = f.object.class.to_s.tableize.singularize = eval "#{table_name}_#{name}_select_options" label_html = app_label name, select_html = f.select name, , include_blank: '----' error_html = app_error f, name, 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) = args. [: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, input_html = f.text_field name, error_html = app_error f, name, 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) = label_txt ? {label_txt: args[0]} : args. value ||= '' value = .delete :value if [:value] label_html = app_label name, 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 = locals. 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! 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 = args. = {} wrap_me = (args.length >= 2 && [Symbol,String,Array].include?(args[0].class) && args[1].is_a?(String)) empty = .delete(:empty) if wrap_me = (.key?(:html) ? [:html] : {}) [:style] = .delete :style [:top] = .delete :top [:bottom] = .delete :bottom [:before] = .delete :before [:after] = .delete :after selector = args.delete_at(0) else before = .delete :before after = .delete :after end [:partial] = args[0] [:locals] = if args.length >= 2 [:collection] = args[1] [:as] = .delete :as if [:as] end render_html = if .key?(:collection) && [:collection].empty? .delete :collection empty ? empty : '' else render end html = if wrap_me wrap selector, render_html, 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... = args. id_args = args.dup id_args << { :namespace => [:namespace] } if .has_key? :namespace target = args.last action = if [:method] == :delete 'delete' else target.new_record? ? 'new' : 'edit' end [,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,| page << "Jenny.show_form_errors('#{form_id}','#{field}','#{}')" 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 = args. render_html = args.shift selector = [selector] unless selector.is_a?(Array) top = .delete :top bottom = .delete :bottom before = .delete :before after = .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 = .delete :class extra_class_names = " #{extra_class_names}" .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 |