jenny 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jenny.gemspec
4
+ gemspec
data/README ADDED
File without changes
@@ -0,0 +1,11 @@
1
+ jenny
2
+ ==========
3
+
4
+ def user_partial user
5
+ html = render partial: 'users/user', locals: { user: user }
6
+ content_tag :div, html, class: 'user'
7
+ end
8
+
9
+ def user_partial user
10
+ parital '.user', user: user
11
+ end
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'jenny/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'jenny'
7
+ s.version = Jenny::VERSION
8
+ s.authors = ['Andrew WC Brown']
9
+ s.email = ['omen.king@gmail.com']
10
+ s.homepage = ''
11
+ s.summary = %q{Useful helper methods for Rails views}
12
+ s.description = %q{Useful helper methods for Rails views}
13
+
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'jenny/version'
2
+ require 'jenny/jenny_helper'
3
+
4
+ module Jenny
5
+ ActionView::Base.send :include, JennyHelper
6
+ end
@@ -0,0 +1,316 @@
1
+ module JennyHelper
2
+ def app_formatted_path args, options
3
+ namespace = []
4
+ namespace << options[:namespace].to_sym if options.has_key? :namespace
5
+ path = namespace + args
6
+ path
7
+ end
8
+
9
+ def app_action_name action, options, element_name=nil
10
+ a_name = []
11
+ a_name << options[:prepend_id].to_sym if options.has_key? :prepend_id
12
+ a_name << action
13
+ a_name << element_name unless element_name.nil?
14
+ a_name = a_name.join('_')
15
+ end
16
+
17
+ def app_form_for *args, &proc
18
+ raise ArgumentError, "Missing block" unless block_given?
19
+ options,id_args,target,action,args = shes_just_a_memory(*args)
20
+ options[:style] ||= 'display: none'
21
+ singular_name = target.class.to_s.underscore
22
+ action_name = app_action_name action, options, 'form'
23
+ form_path = options[:url] || app_formatted_path(args,options)
24
+
25
+ class_name = ['app_form',"#{singular_name}_form","#{action}_#{singular_name}_form"]
26
+ class_name << options[:class] if options[:class]
27
+ class_name = class_name.join ' '
28
+
29
+ loading_args = id_args.clone
30
+ loading_args << (options[:after] ? { after: options[:after] } : {} )
31
+
32
+ html = {
33
+ class: class_name,
34
+ style: options[:style]
35
+ }
36
+
37
+ html.merge! options[:attrs] if options[:attrs]
38
+ form_for target,
39
+ url: form_path,
40
+ remote: true,
41
+ html: html,
42
+ &proc
43
+ end
44
+
45
+ def app_form *args
46
+ options,id_args,target,action,args = shes_just_a_memory *args
47
+ partial_path = []
48
+ partial_path << options[:namespace] if options.has_key? :namespace
49
+ partial_path << target.class.to_s.tableize
50
+ partial_path = partial_path.join('/')
51
+ locals = {}
52
+ args.each{ |a| locals[a.class.to_s.tableize.singularize.to_sym] = a }
53
+ locals.merge! options[:locals] if options[:locals]
54
+ partial "#{partial_path}/form", locals
55
+ end
56
+
57
+
58
+ def show_form_errors target, form_id
59
+ page << "Jenny.remove_form_errors('#{form_id}')"
60
+ target.errors.each do |field,message|
61
+ page << "Jenny.show_form_errors('#{form_id}','#{field}','#{message}')"
62
+ end
63
+ end
64
+
65
+ def shes_just_a_memory(*args)
66
+ #And she use to mean so much to me...
67
+ options = args.extract_options!
68
+ id_args = args.dup
69
+ id_args << { :namespace => options[:namespace] } if options.has_key? :namespace
70
+ target = args.last
71
+ action = if options[:method] == :delete
72
+ 'delete'
73
+ else
74
+ target.new_record? ? 'new' : 'edit'
75
+ end
76
+ [options,id_args,target,action,args]
77
+ end
78
+
79
+ def locals helper_binding, *locals
80
+ options = locals.extract_options!
81
+ result = {}
82
+ vars = eval "local_variables", helper_binding
83
+ for var in vars
84
+ next if var == 'html'
85
+ result[var.to_sym] = eval "#{var}", helper_binding
86
+ end
87
+ result.merge! options
88
+ result.symbolize_keys
89
+ end
90
+
91
+ def app_checkbox f,name
92
+ html = ''
93
+ html << f.label(name, t(".#{name}"))
94
+ html << f.check_box(name)
95
+ wrap '.check_box', html
96
+ end
97
+
98
+ def app_select f, name, *args
99
+ label_txt = args[0].is_a?(String)
100
+ options = label_txt ? {label_txt: args[0]} : args.extract_options!
101
+ any_errors = f.object.errors[name.to_sym].any?
102
+ err_class = '.err' if any_errors
103
+
104
+ table_name = f.object.class.to_s.tableize.singularize
105
+ select_options = eval "#{table_name}_#{name}_select_options"
106
+
107
+ label_html = app_label name, options
108
+ select_html = f.select name, select_options, include_blank: '----'
109
+ error_html = app_error f, name, options
110
+
111
+ html = ''
112
+ html << label_html.to_s
113
+ html << select_html.to_s
114
+ html << error_html.to_s
115
+ html << capture(&block) if block_given?
116
+ html << wrap('.clear')
117
+ html = wrap ".select.#{name}#{err_class}", html
118
+ end
119
+
120
+ def app_expand_select f, name, options={}
121
+ opts = eval "#{f.object.class.to_s.tableize.singularize}_#{name}_select_options"
122
+ html = ''
123
+ html << f.label(name, t(".#{name}"))
124
+ html << wrap('.options', opts.collect{|o|wrap('.option', o[0], :value => o[1])}.join.html_safe)
125
+ html << f.hidden_field(name)
126
+ wrap ".expand_select.#{name}", html
127
+ end
128
+
129
+ def app_label name, options={}
130
+ include_label = options.delete :include_label
131
+ label_txt = options.delete :label_txt
132
+ label_txt = label_txt ? label_txt : t(".#{name}")
133
+ label_tag name, label_txt unless include_label
134
+ end
135
+
136
+ def app_error f, name, options={}
137
+ include_error = options.delete :include_error
138
+ any_errors = f.object.errors[name.to_sym].any?
139
+ error_txt = options.delete :error_txt
140
+ return unless any_errors
141
+ txt = t(".#{name}")
142
+ txt = error_txt if error_txt
143
+ errors = f.object.errors[name.to_sym]
144
+ errors.map!{|e|"#{txt} #{e}"}
145
+ msg = errors.join '<br />'
146
+ wrap ".err_msg.#{name}", msg
147
+ end
148
+
149
+ # params:
150
+ # form_object, name, options
151
+ # form_option, name, label_txt
152
+ #
153
+ # options:
154
+ # include_label (default: true) set false to not render label html
155
+ # include_txt change the label's default text
156
+ # include_error (default: true) set false to not render error html
157
+ def app_text_field f,name, *args, &block
158
+ label_txt = args.shift if args[0].is_a?(String)
159
+ options = args.extract_options!
160
+ options[:label_txt] = label_txt if label_txt
161
+ any_errors = f.object.errors[name.to_sym].any?
162
+ err_class = '.err' if any_errors
163
+
164
+ label_html = app_label name, options
165
+ input_html = f.text_field name, options
166
+ error_html = app_error f, name, options
167
+
168
+ html = ''
169
+ html << label_html.to_s
170
+ html << input_html.to_s
171
+ html << error_html.to_s
172
+ html << capture(&block) if block_given?
173
+ html = wrap ".text_field.#{name}#{err_class}", html
174
+ end
175
+
176
+ def app_password_field f,name,*args
177
+ label_txt = args[0].is_a?(String)
178
+ options = label_txt ? {label_txt: args[0]} : args.extract_options!
179
+ any_errors = f.object.errors[name.to_sym].any?
180
+ err_class = '.err' if any_errors
181
+
182
+ label_html = app_label name, options
183
+ input_html = f.password_field name, options
184
+ error_html = app_error f, name, options
185
+
186
+ html = ''
187
+ html << label_html.to_s
188
+ html << input_html.to_s
189
+ html << error_html.to_s
190
+ html << capture(&block) if block_given?
191
+ html = wrap ".password_field.#{name}#{err_class}", html
192
+ end
193
+
194
+ def app_text_field_tag name, *args
195
+ label_txt = args[0].is_a?(String)
196
+ options = label_txt ? {label_txt: args[0]} : args.extract_options!
197
+ value ||= ''
198
+ value = options.delete :value if options[:value]
199
+
200
+ label_html = app_label name, options
201
+ input_html = text_field_tag name, value
202
+
203
+ html = ''
204
+ html << label_html.to_s
205
+ html << input_html.to_s
206
+ html = wrap ".text_field.#{name}", html
207
+ end
208
+
209
+ def app_text_area f, name
210
+ html = ''
211
+ html << f.label(name, t(".#{name}"))
212
+ html << f.text_area(name)
213
+ wrap ".text_area.#{name}", html
214
+ end
215
+
216
+ def app_hidden_field f, *fields
217
+ fields.collect { |field| f.hidden_field(field) }.join.html_safe
218
+ end
219
+
220
+ def wrap selector, *args
221
+ options = args.extract_options!
222
+
223
+ render_html = args.shift
224
+ selector = [selector] unless selector.is_a?(Array)
225
+
226
+ top = options.delete :top
227
+ bottom = options.delete :bottom
228
+ before = options.delete :before
229
+ after = options.delete :after
230
+
231
+ wrap_render_html = ''
232
+ wrap_render_html << top if top
233
+ wrap_render_html << (render_html.nil? ? '' : render_html)
234
+ wrap_render_html << bottom if bottom
235
+ html = wrap_render_html
236
+ front = ''
237
+ back = []
238
+ for s in selector
239
+ class_name = []
240
+ tag = 'div'
241
+ id = ''
242
+ s = ".#{s}##{s}" if s.is_a?(Symbol)
243
+ s.scan(/^%\w+|\G[\.|\#]\w+/).each do |ss|
244
+ name = ss.reverse.chop.reverse
245
+ case ss[0,1]
246
+ when '.'; class_name << name
247
+ when '#'; id = name
248
+ when '%'; tag = name
249
+ end
250
+ end
251
+ attributes = []
252
+ extra_class_names = options.delete :class
253
+ extra_class_names = " #{extra_class_names}"
254
+ options.each{|k,v|attributes << "#{k}='#{v}'"}
255
+ c = class_name.empty? ? extra_class_names : class_name.join(' ')+extra_class_names
256
+ c = "class='#{c}'"
257
+ i = id.blank? ? '' : "id='#{id}'"
258
+ front << "<#{tag} #{c} #{i} #{attributes.join(' ')}>"
259
+ back << "</#{tag}>"
260
+ end
261
+ render_html = front+html+back.reverse.join('')
262
+ html = ''
263
+ html << before if before
264
+ html << render_html
265
+ html << after if after
266
+ html.html_safe
267
+ end
268
+
269
+ def partial *args
270
+ options = args.extract_options!
271
+ render_options = {}
272
+
273
+ wrap_me = (args.length >= 2 && [Symbol,String,Array].include?(args[0].class) && args[1].is_a?(String))
274
+
275
+ empty = options.delete(:empty)
276
+
277
+ if wrap_me
278
+ html_options = (options.key?(:html) ? options[:html] : {})
279
+ html_options[:style] = options.delete :style
280
+ html_options[:top] = options.delete :top
281
+ html_options[:bottom] = options.delete :bottom
282
+ html_options[:before] = options.delete :before
283
+ html_options[:after] = options.delete :after
284
+
285
+ selector = args.delete_at(0)
286
+ else
287
+ before = options.delete :before
288
+ after = options.delete :after
289
+ end
290
+
291
+ render_options[:partial] = args[0]
292
+ render_options[:locals] = options
293
+ if args.length >= 2
294
+ render_options[:collection] = args[1]
295
+ render_options[:as] = options.delete :as
296
+ end
297
+
298
+ render_html = if render_options.key?(:collection) && render_options[:collection].empty?
299
+ render_options.delete :collection
300
+ empty ? empty : ''
301
+ else
302
+ render render_options
303
+ end
304
+
305
+ html = if wrap_me
306
+ wrap selector, render_html, html_options
307
+ else
308
+ html = ''
309
+ html << before if before
310
+ html << (render_html.nil? ? '' : render_html)
311
+ html << after if after
312
+ html
313
+ end
314
+ html.html_safe
315
+ end
316
+ end
@@ -0,0 +1,3 @@
1
+ module Jenny
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jenny
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Andrew WC Brown
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-07-07 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Useful helper methods for Rails views
17
+ email:
18
+ - omen.king@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - Gemfile
28
+ - README
29
+ - README.md
30
+ - Rakefile
31
+ - jenny.gemspec
32
+ - lib/jenny.rb
33
+ - lib/jenny/jenny_helper.rb
34
+ - lib/jenny/version.rb
35
+ homepage: ""
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.15
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Useful helper methods for Rails views
62
+ test_files: []
63
+