trenni-formatters 2.8.1 → 2.10.0

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.
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,8 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - 2.3
5
- - 2.4
6
- - 2.5
7
- - 2.6
8
- - 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,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,186 +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.for(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
-
47
- context "with key" do
48
- let(:formatter) {FormFormatter.for(double(bar: 10), nested_name: 'attributes')}
49
-
50
- it "should generate form with nested name" do
51
- result = formatter.input(:field => :bar)
52
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><input name="attributes[bar]" value="10"/></dd>}
53
- end
54
- end
55
-
56
- context "with nested object" do
57
- let(:formatter) {FormFormatter.for(double(animal: double(name: "cat")))}
58
-
59
- it "can generate nested attributes" do
60
- result = formatter.nested(:animal) do |formatter|
61
- formatter.input(field: :name)
62
- end
63
-
64
- expect(result).to be == %Q{<dt>Name</dt>\n<dd><input name="animal[name]" value="cat"/></dd>}
65
- end
66
- end
67
- end
68
-
69
- describe "<input>" do
70
- let(:formatter) {FormFormatter.new(:object => double(bar: 10, bob: true, dole: false))}
71
-
72
- it "should support support min, max and step" do
73
- attributes = formatter.input_attributes_for(:min => 10, :max => 20, :step => 2)
74
-
75
- expect(attributes[:min]).to be == 10
76
- expect(attributes[:max]).to be == 20
77
- expect(attributes[:step]).to be == 2
78
-
79
- result = formatter.input(:field => :bar, :min => 10)
80
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10" min="10"/></dd>}
81
- end
82
-
83
- it "should not specify required, readonly or disabled" do
84
- attributes = formatter.input_attributes_for(:field => :bar)
85
- expect(attributes[:required]).to be nil
86
- expect(attributes[:readonly]).to be nil
87
- expect(attributes[:disabled]).to be nil
88
- end
89
-
90
- it "should specify required, readonly or disabled" do
91
- attributes = formatter.input_attributes_for(:field => :bar, :required => true, :readonly => true, :disabled => true)
92
- expect(attributes[:required]).to be true
93
- expect(attributes[:readonly]).to be true
94
- expect(attributes[:disabled]).to be true
95
- end
96
-
97
- it "should generate checked checkbox" do
98
- attributes = formatter.checkbox_attributes_for(:value => true)
99
- expect(attributes[:checked]).to be true
100
-
101
- result = formatter.checkbox(:field => :bob)
102
- 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>}
103
- end
104
-
105
- it "should generate unchecked checkbox" do
106
- attributes = formatter.checkbox_attributes_for(:value => false)
107
- expect(attributes[:checked]).to be nil
108
-
109
- result = formatter.checkbox(:field => :dole)
110
- 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>}
111
- end
112
- end
113
-
114
- describe '<input type="hidden">' do
115
- let(:formatter) {FormFormatter.new(:object => double(age: 20))}
116
-
117
- it "should generate hidden field" do
118
- result = formatter.hidden(:field => :age)
119
- expect(result).to be == %Q{<input type="hidden" name="age" value="20"/>}
120
- end
121
-
122
- it "should escape characters correctly" do
123
- expect(formatter.object).to receive(:new_record?).and_return(false)
124
-
125
- result = formatter.submit
126
- expect(result).to be == %Q{<input type="submit" value="Update"/>}
127
- end
128
-
129
- it "can have custom title" do
130
- result = formatter.submit(title: 'Alice')
131
- expect(result).to be == %Q{<input type="submit" value="Alice"/>}
132
- end
133
- end
134
-
135
- describe '<input type="submit">' do
136
- let(:new_record_formatter) {FormFormatter.new(:object => double(new_record?: true))}
137
- let(:formatter) {FormFormatter.new(:object => double(new_record?: false))}
138
-
139
- it "should have correct title for new_record?" do
140
- result = new_record_formatter.submit
141
- expect(result).to be == %Q{<input type="submit" value="Create"/>}
142
- end
143
-
144
- it "should escape characters correctly" do
145
- result = formatter.submit
146
- expect(result).to be == %Q{<input type="submit" value="Update"/>}
147
- end
148
-
149
- it "can have custom title" do
150
- result = formatter.submit(title: 'Alice')
151
- expect(result).to be == %Q{<input type="submit" value="Alice"/>}
152
- end
153
- end
154
-
155
- describe "<textarea>" do
156
- let(:formatter) {FormFormatter.new(:object => double(details: "foo<bar>"))}
157
-
158
- it "should escape characters correctly" do
159
- result = formatter.textarea(:field => :details)
160
- expect(result).to be == %Q{<dt>\n\tDetails\n</dt>\n<dd><textarea name=\"details\">foo&lt;bar&gt;</textarea></dd>}
161
- end
162
- end
163
-
164
- describe "<output>" do
165
- let(:formatter) {FormFormatter.new(:object => double(bar: 10, bob: true, dole: false))}
166
-
167
- it "should show output value" do
168
- result = formatter.output(:field => :bar)
169
- expect(result).to be == %Q{<dt>Bar</dt>\n<dd><output name=\"bar\">10</output></dd>}
170
- end
171
-
172
- it "should show output value" do
173
- result = formatter.output(:name => :total)
174
- expect(result).to be == %Q{<dt></dt>\n<dd><output name=\"total\"></output></dd>}
175
- end
176
- end
177
-
178
- describe "<button>" do
179
- let(:formatter) {FormFormatter.new}
180
-
181
- it "should generate reset button" do
182
- result = formatter.button(:type => :reset)
183
- expect(result).to be == %Q{<button type="reset">Reset</button>}
184
- end
185
- end
186
- 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