trenni-formatters 2.5.0 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --color
2
- --format documentation
3
- --backtrace
4
- --warnings
data/.simplecov DELETED
@@ -1,9 +0,0 @@
1
-
2
- SimpleCov.start do
3
- add_filter "/spec/"
4
- end
5
-
6
- if ENV['TRAVIS']
7
- require 'coveralls'
8
- Coveralls.wear!
9
- end
@@ -1,14 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- rvm:
4
- - 2.1.8
5
- - 2.2.4
6
- - 2.3.0
7
- - 2.4.0
8
- - ruby-head
9
- - rbx-2
10
- env: COVERAGE=true
11
- matrix:
12
- allow_failures:
13
- - rvm: "rbx-2"
14
- - rvm: "ruby-head"
data/Gemfile DELETED
@@ -1,12 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in trenni-formatters.gemspec
4
- gemspec
5
-
6
- group :test do
7
- gem 'sanitize'
8
- gem 'kramdown'
9
-
10
- gem 'simplecov'
11
- gem 'coveralls', require: false
12
- end
data/README.md DELETED
@@ -1,77 +0,0 @@
1
- # Trenni::Formatters
2
-
3
- Trenni is a templating system, and these formatters assist with the development
4
- of typical view and form based web interface. A formatter is a high-level
5
- adapter that turns model data into presentation text.
6
-
7
- Formatters are designed to be customised, typically per-project, for specific
8
- formatting needs.
9
-
10
- [![Build Status](https://travis-ci.org/ioquatix/trenni-formatters.svg?branch=master)](https://travis-ci.org/ioquatix/trenni-formatters)
11
- [![Code Climate](https://codeclimate.com/github/ioquatix/trenni-formatters.png)](https://codeclimate.com/github/ioquatix/trenni-formatters)
12
- [![Coverage Status](https://coveralls.io/repos/ioquatix/trenni-formatters/badge.svg)](https://coveralls.io/r/ioquatix/trenni-formatters)
13
-
14
- ## Motivation
15
-
16
- `Trenni::Formatters` was a library extracted from [Financier](https://github.com/ioquatix/financier) which is a CouchDB backed invoicing system, itself, derived from an old Rails app. I was a bit envious of `form_for` in terms of the ease of generating forms, but found that it wasn't very extendable. I also found myself generating code to format model data as rich HTML. `Trenni::Formatters` attempts to be an easily extendable formatting module which can generate rich text, links and HTML.
17
-
18
- ## Installation
19
-
20
- Add this line to your application's Gemfile:
21
-
22
- gem 'trenni-formatters'
23
-
24
- And then execute:
25
-
26
- $ bundle
27
-
28
- Or install it yourself as:
29
-
30
- $ gem install trenni-formatters
31
-
32
- ## Usage
33
-
34
- The most basic usage involves converting model data into presentation text by
35
- a mapping corresponding to the objects type:
36
-
37
- formatter = Trenni::Formatters::Formatter.new
38
-
39
- formatter.for(String) do |value, options|
40
- "String: #{value}"
41
- end
42
-
43
- expect(formatter.format("foobar")).to be == "String: foobar"
44
-
45
- For more examples please see `spec/`.
46
-
47
- ## Contributing
48
-
49
- 1. Fork it
50
- 2. Create your feature branch (`git checkout -b my-new-feature`)
51
- 3. Commit your changes (`git commit -am 'Add some feature'`)
52
- 4. Push to the branch (`git push origin my-new-feature`)
53
- 5. Create new Pull Request
54
-
55
- ## License
56
-
57
- Released under the MIT license.
58
-
59
- Copyright, 2014, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
60
-
61
- Permission is hereby granted, free of charge, to any person obtaining a copy
62
- of this software and associated documentation files (the "Software"), to deal
63
- in the Software without restriction, including without limitation the rights
64
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
65
- copies of the Software, and to permit persons to whom the Software is
66
- furnished to do so, subject to the following conditions:
67
-
68
- The above copyright notice and this permission notice shall be included in
69
- all copies or substantial portions of the Software.
70
-
71
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
74
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
75
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
76
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
77
- THE SOFTWARE.
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec) do |task|
5
- task.rspec_opts = ["--require", "simplecov"] if ENV['COVERAGE']
6
- end
7
-
8
- task :default => :spec
@@ -1,165 +0,0 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'trenni/formatters'
22
- require 'trenni/formatters/html/definition_list_form'
23
-
24
- module Trenni::Formatters::FormFormatterSpec
25
- class FormFormatter < Trenni::Formatters::Formatter
26
- include Trenni::Formatters::HTML::DefinitionListForm
27
- end
28
-
29
- describe Trenni::Formatters do
30
- let(:formatter) {FormFormatter.new(:object => double(:bar => 10))}
31
-
32
- it "should generate form" do
33
- result = formatter.input(:field => :bar)
34
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10"/></dd>}
35
- end
36
-
37
- it "should have default value" do
38
- expect(formatter.value_for(:value => 10)).to be == "10"
39
- expect(formatter.value_for(:value => nil, :default => 10)).to be == "10"
40
- end
41
-
42
- it "should have a different title" do
43
- result = formatter.input(:field => :bar, :title => "Title")
44
- expect(result).to be == %Q{<dt>Title</dt>\n<dd><input name="bar" value="10"/></dd>}
45
- end
46
- end
47
-
48
- describe "<input>" do
49
- let(:formatter) {FormFormatter.new(:object => double(bar: 10, bob: true, dole: false))}
50
-
51
- it "should support support min, max and step" do
52
- attributes = formatter.input_attributes_for(:min => 10, :max => 20, :step => 2)
53
-
54
- expect(attributes[:min]).to be == 10
55
- expect(attributes[:max]).to be == 20
56
- expect(attributes[:step]).to be == 2
57
-
58
- result = formatter.input(:field => :bar, :min => 10)
59
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10" min="10"/></dd>}
60
- end
61
-
62
- it "should not specify required, readonly or disabled" do
63
- attributes = formatter.input_attributes_for(:field => :bar)
64
- expect(attributes[:required]).to be nil
65
- expect(attributes[:readonly]).to be nil
66
- expect(attributes[:disabled]).to be nil
67
- end
68
-
69
- it "should specify required, readonly or disabled" do
70
- attributes = formatter.input_attributes_for(:field => :bar, :required => true, :readonly => true, :disabled => true)
71
- expect(attributes[:required]).to be true
72
- expect(attributes[:readonly]).to be true
73
- expect(attributes[:disabled]).to be true
74
- end
75
-
76
- it "should generate checked checkbox" do
77
- attributes = formatter.checkbox_attributes_for(:value => true)
78
- expect(attributes[:checked]).to be true
79
-
80
- result = formatter.checkbox(:field => :bob)
81
- expect(result).to be == %Q{<dd>\n\t<input type="hidden" name="bob" value="false"/>\n\t<label><input type="checkbox" name="bob" value="true" checked/> Bob</label>\n</dd>}
82
- end
83
-
84
- it "should generate unchecked checkbox" do
85
- attributes = formatter.checkbox_attributes_for(:value => false)
86
- expect(attributes[:checked]).to be nil
87
-
88
- result = formatter.checkbox(:field => :dole)
89
- expect(result).to be == %Q{<dd>\n\t<input type="hidden" name="dole" value="false"/>\n\t<label><input type="checkbox" name="dole" value="true"/> Dole</label>\n</dd>}
90
- end
91
- end
92
-
93
- describe '<input type="hidden">' do
94
- let(:formatter) {FormFormatter.new(:object => double(age: 20))}
95
-
96
- it "should generate hidden field" do
97
- result = formatter.hidden(:field => :age)
98
- expect(result).to be == %Q{<input type="hidden" name="age" value="20"/>}
99
- end
100
-
101
- it "should escape characters correctly" do
102
- expect(formatter.object).to receive(:new_record?).and_return(false)
103
-
104
- result = formatter.submit
105
- expect(result).to be == %Q{<input type="submit" value="Update"/>}
106
- end
107
-
108
- it "can have custom title" do
109
- result = formatter.submit(title: 'Alice')
110
- expect(result).to be == %Q{<input type="submit" value="Alice"/>}
111
- end
112
- end
113
-
114
- describe '<input type="submit">' do
115
- let(:new_record_formatter) {FormFormatter.new(:object => double(new_record?: true))}
116
- let(:formatter) {FormFormatter.new(:object => double(new_record?: false))}
117
-
118
- it "should have correct title for new_record?" do
119
- result = new_record_formatter.submit
120
- expect(result).to be == %Q{<input type="submit" value="Create"/>}
121
- end
122
-
123
- it "should escape characters correctly" do
124
- result = formatter.submit
125
- expect(result).to be == %Q{<input type="submit" value="Update"/>}
126
- end
127
-
128
- it "can have custom title" do
129
- result = formatter.submit(title: 'Alice')
130
- expect(result).to be == %Q{<input type="submit" value="Alice"/>}
131
- end
132
- end
133
-
134
- describe "<textarea>" do
135
- let(:formatter) {FormFormatter.new(:object => double(details: "foo<bar>"))}
136
-
137
- it "should escape characters correctly" do
138
- result = formatter.textarea(:field => :details)
139
- expect(result).to be == %Q{<dt>\n\tDetails\n</dt>\n<dd><textarea name=\"details\">foo&lt;bar&gt;</textarea></dd>}
140
- end
141
- end
142
-
143
- describe "<output>" do
144
- let(:formatter) {FormFormatter.new(:object => double(bar: 10, bob: true, dole: false))}
145
-
146
- it "should show output value" do
147
- result = formatter.output(:field => :bar)
148
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><output name=\"bar\">10</output></dd>}
149
- end
150
-
151
- it "should show output value" do
152
- result = formatter.output(:name => :total)
153
- expect(result).to be == %Q{<dt></dt>\n<dd><output name=\"total\"></output></dd>}
154
- end
155
- end
156
-
157
- describe "<button>" do
158
- let(:formatter) {FormFormatter.new}
159
-
160
- it "should generate reset button" do
161
- result = formatter.button(:type => :reset)
162
- expect(result).to be == %Q{<button type="reset">Reset</button>}
163
- end
164
- end
165
- end
@@ -1,57 +0,0 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'trenni/formatters'
22
-
23
- module Trenni::FormattersSpec
24
- class TestFormatter < Trenni::Formatters::Formatter
25
- def initialize(*)
26
- @count = 0
27
-
28
- super
29
- end
30
-
31
- attr :count
32
-
33
- map(String) do |value, options|
34
- @count += 1
35
-
36
- "String: #{value}"
37
- end
38
- end
39
-
40
- describe Trenni::Formatters do
41
- let(:test_formatter) {TestFormatter.new(foo: :bar)}
42
-
43
- it "should format string" do
44
- expect(test_formatter.format("foobar")).to be == "String: foobar"
45
- expect(test_formatter.count).to be == 1
46
- end
47
-
48
- it "should format numbers" do
49
- expect(test_formatter.format(10)).to be == "10"
50
- expect(test_formatter.count).to be == 0
51
- end
52
-
53
- it "has options" do
54
- expect(test_formatter[:foo]).to be == :bar
55
- end
56
- end
57
- end
@@ -1,129 +0,0 @@
1
- # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'trenni/formatters'
22
- require 'trenni/formatters/html/definition_list_form'
23
- require 'trenni/formatters/html/option_select'
24
-
25
- module Trenni::Formatters::HTML::OptionSelectSpec
26
- class FormFormatter < Trenni::Formatters::Formatter
27
- include Trenni::Formatters::HTML::DefinitionListForm
28
-
29
- def select(options = {}, &block)
30
- element(Trenni::Formatters::HTML::OptionSelect, options, &block)
31
- end
32
- end
33
-
34
- def capture(&block)
35
- _out = ""
36
-
37
- Trenni::Template.capture do
38
- formatter.select :field => :bar do |select|
39
- _out << select.item(:title => "A", :value => 0)
40
- _out << select.item(:title => "B", :value => 10)
41
- end
42
- end
43
- end
44
-
45
- describe Trenni::Formatters::HTML::OptionSelect do
46
- let(:formatter) {FormFormatter.new(:object => double(:bar => 10))}
47
-
48
- it "should list items" do
49
- _out = ""
50
-
51
- captured = Trenni::Template.capture do
52
- formatter.select :field => :bar do |select|
53
- _out << select.item(:title => "A", :value => 0)
54
- _out << select.item(:title => "B", :value => 10)
55
- end
56
- end
57
-
58
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar\">\n\t\t<option value=\"0\">A</option><option value=\"10\" selected>B</option>\n\t</select>\n</dd>"
59
- end
60
-
61
- it "should list items with data attributes" do
62
- _out = ""
63
-
64
- captured = Trenni::Template.capture do
65
- formatter.select :field => :bar do |select|
66
- _out << select.item(:title => "A", :value => 0, :data => {foo: 'bar'})
67
- end
68
- end
69
-
70
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar\">\n\t\t<option value=\"0\" data-foo=\"bar\">A</option>\n\t</select>\n</dd>"
71
- end
72
-
73
- it "should list items for multiple selection" do
74
- _out = ""
75
-
76
- captured = Trenni::Template.capture do
77
- formatter.select :field => :bar, multiple: true do |select|
78
- _out << select.item(:title => "A", :value => 0)
79
- _out << select.item(:title => "B", :value => 10)
80
- end
81
- end
82
-
83
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar[]\" multiple>\n\t\t<option value=\"0\">A</option><option value=\"10\" selected>B</option>\n\t</select>\n</dd>"
84
- end
85
-
86
- it "should add optional item" do
87
- _out = ""
88
-
89
- captured = Trenni::Template.capture do
90
- formatter.select :field => :bar, optional: true do |select|
91
- _out << select.item(:title => "A", :value => 0)
92
- _out << select.item(:title => "B", :value => 10)
93
- end
94
- end
95
-
96
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar\">\n\t\t<option></option>\t\t<option value=\"0\">A</option><option value=\"10\" selected>B</option>\n\t</select>\n</dd>"
97
- end
98
-
99
- it "should add optional item in group" do
100
- _out = ""
101
-
102
- captured = Trenni::Template.capture do
103
- formatter.select :field => :bar do |select|
104
- select.group(title: 'group', optional: true) do
105
- _out << select.item(:title => "A", :value => 0)
106
- end
107
- _out << select.item(:title => "B", :value => 10)
108
- end
109
- end
110
-
111
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar\">\n\t\t<optgroup label=\"group\">\n\t\t\t<option></option>\t\t\t<option value=\"0\">A</option>\n\t\t</optgroup>\t\t<option value=\"10\" selected>B</option>\n\t</select>\n</dd>"
112
- end
113
-
114
- it "should add a group" do
115
- _out = ""
116
-
117
- captured = Trenni::Template.capture do
118
- formatter.select :field => :bar do |select|
119
- select.group(title: 'group') do
120
- _out << select.item(:title => "A", :value => 0)
121
- end
122
- _out << select.item(:title => "B", :value => 10)
123
- end
124
- end
125
-
126
- expect(captured).to be == "<dt>Bar</dt>\n<dd>\n\t<select name=\"bar\">\n\t\t<optgroup label=\"group\">\n\t\t\t<option value=\"0\">A</option>\n\t\t</optgroup>\t\t<option value=\"10\" selected>B</option>\n\t</select>\n</dd>"
127
- end
128
- end
129
- end