lrd_view_tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Logical Reality Design and Evan Dorn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ =LRD ViewTools
2
+
3
+ Standard view helpers and stylesheet defaults for Logical Reality Design
4
+ Projects.
5
+
6
+ == Installing
7
+
8
+ Add to Gemfile:
9
+ gem 'lrd_view_tools'
10
+
11
+ and run 'bundle install'.
12
+
13
+ Also, 'rails generate lrd_view_tools:install' will copy in default images,
14
+ stylesheets, and helper partials to the rails app.
15
+
16
+ == Simple labeled inputs for forms:
17
+
18
+ labeled_input(form, field)
19
+
20
+ Produces a <label> <input> pair for a text field in a standard form helper.
21
+ The included stylesheet forms.sass will format the label as an 8-em wide
22
+ float to the left of the input.
23
+
24
+ Usage example:
25
+ form_for(@user) do |form
26
+ = labeled_input(form, :name)
27
+ = labeled_input(form, :email)
28
+ = labeled_input(form, nil, :input => form.submit("Save Changes"))
29
+ end
30
+
31
+ Options:
32
+ The output defaults to <input type="text" name="#{field}"> for the
33
+ but can be overridden in a number of ways:
34
+
35
+ #Override the form element:
36
+ labeled_input(form, :boolean_field, :input => form.checkbox(:boolean_field))
37
+
38
+ #Force a blank label:
39
+ labeled_input(form, :fieldname, :nolabel => true )
40
+
41
+ #Alter label text:
42
+ labeled_input(form, :fieldname, :text => "Other Text")
43
+
44
+ #Add a comment after the input:
45
+ labeled_input(form, :password_confirmation, :comment => "Re-enter password")
46
+
47
+ == Pass a block to partial:
48
+
49
+ block_to_partial(partial_name, options = {}, &block)
50
+
51
+ Usage example:
52
+ block_to_partial('shared/my_partial') do
53
+ <p>Paragraph to be yielded inside the partial.</p>
54
+ end
55
+
56
+ The block will be available to the partial as 'body', and any options
57
+ will be passed through as locals. In addition, options[:id] and
58
+ options[:class] will get converted to locals 'cssid' and 'cssclass'
59
+ to avoid name collisions.
60
+
61
+ == A better debug()
62
+
63
+ lrd_debug(var) will output var, wrapped in <pre></pre>, but using a
64
+ recursive pp output to handle large objects.
65
+
66
+ Usage example (in HAML):
67
+ if ENV["RAILS_ENV"] == development
68
+ #debug
69
+ %h2 Request environment:
70
+ = lrd_debug(request.env)
71
+
72
+ Copyright (c) 2010-2011 Evan Dorn, released under the MIT license
@@ -0,0 +1,30 @@
1
+ module LRD
2
+ module DebugHelper
3
+ require 'pp'
4
+ require 'stringio'
5
+
6
+ def lrd_debug(object)
7
+ "<pre>#{h(pp_s(object))}</pre>"
8
+ end
9
+
10
+ def pp_s(*objs)
11
+ s = StringIO.new
12
+ objs.each {|obj|
13
+ PP.pp(obj, s)
14
+ }
15
+ s.rewind
16
+ s.read
17
+ end
18
+ alias :pp_to_s :pp_s
19
+
20
+ def debug_link(name)
21
+ link_to name.titleize, '#', :onclick => "Element.toggle('#{name}_debug_info'); return false;"
22
+ end
23
+ def debug_block(name, &block)
24
+ content = capture(&block)
25
+ title = content_tag(:h2, name.titleize)
26
+ concat(content_tag :fieldset, content, {:id => "#{name}_debug_info", :style => 'display: none;' } )
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,150 @@
1
+
2
+ module LRD
3
+ module FormHelper
4
+
5
+ def self.included(arg)
6
+ p "LRD::FormHelper included in #{arg}"
7
+ ActionView::Helpers::FormBuilder.send(:include, LRD::FormBuilder)
8
+ end
9
+
10
+ # Returns a <div> containing a label, an input, and an option comment
11
+ # block, pre-styled in LRD style.
12
+ #
13
+ # pass :label => false to suppress the label text. (A label tag is still emitted.)
14
+ # pass :required => true to dispay as a required field
15
+ # pass :text => "foo" to override the label text
16
+ # pass :class => 'foo' to add 'foo' to the CSS class of the <div>
17
+ # pass :comment => "text" to append a span.comment with text after the input
18
+ # pass :input_type => 'password' } to use a password_field instead of a text_field
19
+ # (also supported: text, passsword, hidden, file, text_area, search, telephone, url
20
+ # email, range, submit)
21
+ #
22
+ # ==== Examples (in HAML):
23
+ # - form_for(@user) do
24
+ # = f.labeled_input(:login)
25
+ # # => <div class='labeled_input'>
26
+ # # => <label for='user_login'>login</label>
27
+ # # => <input type='text' name='user[login]' id='user_login' value="#{@user.login}" />
28
+ # # => </div>
29
+ def labeled_input(object_name, method, options = {}, &block)
30
+ divclass = labeled_input_divclass(options)
31
+ comment = comment_for_labeled_input(options.delete(:comment))
32
+ if block_given?
33
+ input = yield
34
+ else
35
+ input = input_for_labeled_input(object_name, method, options)
36
+ end
37
+
38
+ if object_name.blank? or method.blank?
39
+ label = "<label>&nbsp;</label>".html_safe
40
+ elsif text = options.delete(:text)
41
+ label = label(object_name, method, text, options)
42
+ else
43
+ label = label(object_name, method, options)
44
+ end
45
+ content_tag(:div, (label + input + comment), { :class => divclass })
46
+ end
47
+
48
+ def comment_for_labeled_input(text)
49
+ if text
50
+ content_tag( :span, { :class => 'comment' } ) { text }
51
+ else
52
+ ""
53
+ end
54
+ end
55
+
56
+
57
+ def labeled_input_divclass(options)
58
+ cssclass = "labeled_input"
59
+ cssclass += " required" if options[:required]
60
+ cssclass += " #{options[:class]}" if options[:class]
61
+ cssclass
62
+ end
63
+
64
+ def input_for_labeled_input(object_name, method, options)
65
+ case input_type = options.delete(:input_type).to_s
66
+ when "text", ""
67
+ input = text_field( object_name, method, options)
68
+ when "password"
69
+ input = password_field( object_name, method, options)
70
+ when "hidden"
71
+ input = hidden_field( object_name, method, options)
72
+ when "file"
73
+ input = file_field( object_name, method, options)
74
+ when "text_area"
75
+ input = text_area( object_name, method, options)
76
+ when "search"
77
+ input = search_field( object_name, method, options)
78
+ when "telephone"
79
+ input = telephone_field(object_name, method, options)
80
+ when "url"
81
+ input = url_field( object_name, method, options)
82
+ when "email"
83
+ input = email_field( object_name, method, options)
84
+ when "number"
85
+ input = number_field( object_name, method, options)
86
+ when "range"
87
+ input = range_field( object_name, method, options)
88
+ when "submit"
89
+ input = submit_tag( options[:submit_text], options)
90
+ else
91
+ raise "labeled_input input_type #{input_type} is not a valid type!"
92
+ end
93
+ input
94
+ end
95
+
96
+ def unlabeled_input(object_name, method, options)
97
+ labeled_input(object_name, method, options.merge!(:label => false))
98
+ end
99
+
100
+ # creates a submit button that lines up with a bunch of labeled_input fields
101
+ def unlabeled_submit(text = nil)
102
+ labeled_input(nil, nil, :input_type => :submit, :submit_text => text)
103
+ end
104
+ end
105
+ end
106
+
107
+ # # pass { :nolabel => true } to replace the label with a spacer
108
+ # # pass { :required => true } to dispay as a required field
109
+ # # pass { :text => "foo" } to override the label text
110
+ # # pass { :class => 'foo'} to add 'foo' to the CSS class of the <div>
111
+ # #
112
+ # # input_options hash gets passed directly to the input field
113
+ # def labeled_input(form, field, options = {}, input_options = {})
114
+ # options[:text] = "&nbsp;".html_safe if options[:nolabel]
115
+ # options.reverse_merge!(:text => nil,:required => false, :nolabel => false)
116
+ # options.merge!(:form => form, :field => field)
117
+ # input_options.reverse_merge!( :size => 30 )
118
+ #
119
+ # cssclass = "labeled_input"
120
+ # cssclass += " required" if options[:required]
121
+ # cssclass += " #{options[:class]}" if options[:class]
122
+ #
123
+ # unless input = options[:input]
124
+ # input = form.text_field field, input_options
125
+ # end
126
+ #
127
+ # if field.blank?
128
+ # label = (content_tag :label, options[:text]).html_safe
129
+ # else
130
+ # label = (form.label field, options[:text]).html_safe
131
+ # end
132
+ # comment = options[:comment] ? content_tag( :span, { :class => 'comment' } ) { options[:comment] } : ""
133
+ #
134
+ # content_tag(:div, (label + input + comment), { :class => cssclass }).html_safe
135
+ # end
136
+ # f.labeled_input(stuff, :input_type => :hidden)
137
+ #
138
+ # f.labeled_input(stuff){ f.hidden_field(stuff) }
139
+ #
140
+
141
+ module LRD::FormBuilder
142
+ def labeled_input(method, options = {})
143
+ @template.labeled_input(@object_name, method, objectify_options(options))
144
+ end
145
+ def unlabeled_submit(text = nil)
146
+ @template.unlabeled_submit(text)
147
+ end
148
+ end
149
+
150
+
@@ -0,0 +1,52 @@
1
+ module LRD
2
+ module ViewHelper
3
+
4
+ # Stores a headline for later rendering by the layout
5
+ def set_headline(headline)
6
+ content_for(:headline, headline)
7
+ end
8
+
9
+ # displays a checkmark if the field is set true
10
+ def bool_checked(field)
11
+ filename = field ? "check.png" : "blank.gif"
12
+ image_tag(filename, :alt => "yes", :size => "16x16")
13
+ end
14
+
15
+
16
+ # Passes the supplied block to the named partial
17
+ def block_to_partial(partial_name, options = {}, &block)
18
+ # replace :id with :cssid and :class with :cssclass
19
+ if options[:id]
20
+ options[:cssid] = options.delete(:id)
21
+ else
22
+ options[:cssid] = "" if options[:cssid].nil?
23
+ end
24
+ if options[:class]
25
+ options[:cssclass] = options.delete(:class)
26
+ else
27
+ options[:cssclass] = "" if options[:cssclass].nil?
28
+ end
29
+
30
+ options.merge!(:body => capture(&block))
31
+ render(:partial => partial_name, :locals => options)
32
+ end
33
+
34
+ # a standardized view helper that renders a box with an optional
35
+ # title. The standard partial for it is in views/shared/_page_block.html.haml
36
+ def page_block(title = nil, options = {}, &block)
37
+ block_to_partial('shared/block', options.merge(:title => title), &block).html_safe
38
+ end
39
+
40
+ end
41
+
42
+ def debug_link(name)
43
+ link_to name.titleize, '#', :onclick => "Element.toggle('#{name}_debug_info'); return false;"
44
+ end
45
+ def debug_block(name, &block)
46
+ content = capture(&block)
47
+ title = content_tag(:h2, name.titleize)
48
+ concat(content_tag :fieldset, content, {:id => "#{name}_debug_info", :style => 'display: none;' } )
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), 'app', 'helpers', 'lrd_view_helper')
2
+ require File.join(File.dirname(__FILE__), 'app', 'helpers', 'lrd_debug_helper')
3
+ require File.join(File.dirname(__FILE__), 'app', 'helpers', 'lrd_form_helper')
4
+
5
+ module LRD::DevTools
6
+ class Railtie < Rails::Railtie
7
+ ActionController::Base.helper(LRD::ViewHelper)
8
+ ActionController::Base.helper(LRD::DebugHelper)
9
+ ActionView::Helpers::FormHelper.send(:include, LRD::FormHelper)
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'lrd_view_helper'
3
+
4
+ describe LRD::ViewHelper do
5
+
6
+ describe "bool_checked" do
7
+ describe "with true" do
8
+ it "should return an image tag for the checkmark" do
9
+ helper.bool_checked(true).should =~ /images\/check.png/
10
+ end
11
+ end
12
+ describe "with false" do
13
+ it "should return an image tag for a spacer" do
14
+ helper.bool_checked(false).should =~ /images\/blank.gif/
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "labeled_input" do
20
+ before(:each) do
21
+ @form = mock(ActionView::Helpers::FormHelper)
22
+ @form.stub!(:text_field).and_return("<input name='field' />")
23
+ @form.stub!(:label).and_return("<label for='field'>")
24
+
25
+ @labeled_input = helper.labeled_input(@form, :field)
26
+ end
27
+
28
+ it "should return a string" do
29
+ @labeled_input.is_a?(String).should be_true
30
+ end
31
+ it "should contain the input tag" do
32
+ @labeled_input.should match(/<input/)
33
+ @labeled_input.should match(/name='field'/)
34
+ end
35
+ it "should contain the label tag field" do
36
+ @labeled_input.should match(/<label/)
37
+ @labeled_input.should match(/for='field'/)
38
+ end
39
+
40
+ end
41
+
42
+
43
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec/spec_helper'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib','app', 'helpers', 'lrd_form_helper')
3
+ ActionView::Helpers::FormHelper.send(:include, LRD::FormHelper)
4
+
5
+ describe "form_for().labelled_input", :type => :view do
6
+ it "should render an inline template" do
7
+ render :inline => "<%= who%> rocks!", :locals => {:who => "Judson"}
8
+ rendered.should == "Judson rocks!"
9
+ end
10
+
11
+ let :user do
12
+ view.stub!(:user_path => "#")
13
+ mock_model("User", :login => "Username")
14
+ end
15
+
16
+ it "should render a labeled_input successfully" do
17
+ render(:inline => <<-EOTEMPLATE, :locals => { :user => user })
18
+ <%= form_for(user) do |f| %>
19
+ <%= f.labeled_input(:login) %>
20
+ <%- end -%>
21
+ EOTEMPLATE
22
+ p "Rendered is: ", rendered
23
+ rendered.should_not be_nil
24
+
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ # include LRDViewHelpers
4
+
5
+ describe "LRDViewHelpers" do
6
+ it "should have a pending spec"
7
+
8
+
9
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,18 @@
1
+ require 'active_support'
2
+ require 'active_support/deprecation'
3
+ require 'action_view'
4
+ require 'rspec/rails/adapters'
5
+ require 'rspec/rails/example/rails_example_group'
6
+ require 'rspec/rails/matchers/render_template'
7
+ require 'rspec/rails/browser_simulators'
8
+ require 'rspec/rails/example/view_example_group'
9
+ require 'rspec/rails/mocks'
10
+
11
+ plugin_spec_dir = File.dirname(__FILE__)
12
+ #ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
13
+
14
+ RSpec::configure do |c|
15
+ c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
16
+ :file_path => 'spec/views'
17
+ }
18
+ end
@@ -0,0 +1,11 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ # puts "You need to install rspec in your base app"
5
+ # exit
6
+ end
7
+
8
+ require 'rspec/rails/example/view_example_group'
9
+
10
+ plugin_spec_dir = File.dirname(__FILE__)
11
+ #ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
@@ -0,0 +1,10 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ plugin_spec_dir = File.dirname(__FILE__)
9
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lrd_view_tools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Evan Dorn
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-22 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Compatible with Rails 3.x. Don't even bother trying with Rails 2.x or before.'
23
+ email: evan@lrdesign.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE.txt
30
+ - README.rdoc
31
+ files:
32
+ - LICENSE.txt
33
+ - README.rdoc
34
+ - lib/app/helpers/lrd_debug_helper.rb
35
+ - lib/app/helpers/lrd_form_helper.rb
36
+ - lib/app/helpers/lrd_view_helper.rb
37
+ - lib/lrd_view_tools.rb
38
+ - spec/helpers/lrd_view_helper_spec.rb
39
+ - spec/labelled_input_spec.rb
40
+ - spec/lrd_view_tools_spec.rb
41
+ - spec/spec.opts
42
+ - spec/spec_helper.rb
43
+ - spec/spec_helper_naked.rb
44
+ - spec/spec_helper_plugin.rb
45
+ has_rdoc: true
46
+ homepage: http://LRDesign.com
47
+ licenses:
48
+ - MIT
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib/
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.5.1
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: View helpers and defaults for LRD projects.
79
+ test_files: []
80
+