trenni-formatters 2.7.0 → 2.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 54e55baacc3ffd8e0fd529f9261d3d953c8500df
4
- data.tar.gz: 5750df51c9534a3794af5a596ef54861c8d234b3
2
+ SHA256:
3
+ metadata.gz: d037e40cc445eb738842fe5d21e393b3b9166a4cd0cc16f663a68d89748029c3
4
+ data.tar.gz: 971da30b39aa8c5bfb40d2838ef09083abb25ff054e6ab8d4dfc63b405a5d2dc
5
5
  SHA512:
6
- metadata.gz: dd169596c9742758211f18b22c7a207fc746094f774113ec982d372537d377b73443c3b9bea017eaa52d1f1cae6b919ffaf0210252a64dcd8becc8f1cc9a6f78
7
- data.tar.gz: fc24cb66fb14cb62a0505de3594cc0b31c2a9ff339459ca1554a52b74589d507809a81baa4ca19c1b2b7f621ae311d296204f441be58135e3b8a915fa0736ab8
6
+ metadata.gz: dab2480c97459bbb95ec16b6c68a006d6cf513ad3888f0484d38b2136a0ea8c62eb948692ecc8d573a98a4030e96365b3b6dfef3fd9aab7229eb7bddffdeef26
7
+ data.tar.gz: af3b1cf1cb56ae17c96c1ba08b3975c6667a30b4e303a61453132fb5bf977b630eca7ea9224e5b45fd1e75d096705f7908d233ea8a22d600d9802711d60a3568
data/.travis.yml CHANGED
@@ -1,14 +1,8 @@
1
1
  language: ruby
2
- sudo: false
2
+
3
3
  rvm:
4
- - 2.1.8
5
- - 2.2.4
6
- - 2.3.0
7
- - 2.4.0
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
8
  - ruby-head
9
- - rbx-2
10
- env: COVERAGE=true
11
- matrix:
12
- allow_failures:
13
- - rvm: "rbx-2"
14
- - rvm: "ruby-head"
@@ -25,12 +25,54 @@ require 'mapping/descendants'
25
25
  module Trenni
26
26
  module Formatters
27
27
  class Formatter < Mapping::Model
28
- def initialize(options = {})
28
+ def self.for(object, **options)
29
+ self.new(object: object, **options)
30
+ end
31
+
32
+ def initialize(**options)
29
33
  @options = options
30
34
  end
31
35
 
36
+ # The target object of the form.
37
+ def object
38
+ @object ||= @options[:object]
39
+ end
40
+
41
+ # The name of the field, used for the name attribute of an input.
42
+ def name_for(**options)
43
+ name = options[:name] || options[:field]
44
+
45
+ if suffix = options[:suffix]
46
+ name = "#{name}#{suffix}"
47
+ end
48
+
49
+ if nested_name = options[:nested_name]
50
+ "#{nested_name}[#{name}]"
51
+ else
52
+ name
53
+ end
54
+ end
55
+
56
+ def nested_name_for(**options)
57
+ name_for(**options)
58
+ end
59
+
60
+ def nested(name, key = name, klass: self.class)
61
+ options = @options.dup
62
+ target = self.object.send(name)
63
+
64
+ options[:object] = target
65
+ options[:nested_name] = nested_name_for(name: key)
66
+
67
+ formatter = klass.new(**options)
68
+
69
+ return formatter unless block_given?
70
+
71
+ yield formatter
72
+ end
73
+
32
74
  attr :options
33
-
75
+
34
76
  def format_unspecified(object, options)
35
77
  object.to_s
36
78
  end
@@ -36,14 +36,14 @@ module Trenni
36
36
  Builder.fragment do |builder|
37
37
  builder.inline(:dt) do
38
38
  builder.text title_for(options)
39
-
40
- if details = details_for(options)
41
- builder.inline(:small) { builder.text details }
42
- end
43
39
  end
44
40
 
45
41
  builder.inline(:dd) do
46
42
  builder.tag :input, input_attributes_for(options)
43
+
44
+ if details = details_for(options)
45
+ builder.inline(:small, class: 'details') { builder.text details }
46
+ end
47
47
  end
48
48
  end
49
49
  end
@@ -72,7 +72,7 @@ module Trenni
72
72
  builder.text title_for(options)
73
73
 
74
74
  if details = details_for(options)
75
- builder.inline(:small) { builder.text details }
75
+ builder.inline(:small, class: 'details') { builder.text details }
76
76
  end
77
77
  end
78
78
 
@@ -91,11 +91,16 @@ module Trenni
91
91
  Builder.fragment do |builder|
92
92
  builder.tag(:dd) do
93
93
  builder.tag :input, :type => :hidden, :name => name_for(options), :value => 'false'
94
+
94
95
  builder.inline(:label) do
95
96
  builder.tag :input, checkbox_attributes_for(options)
96
97
  # We would like a little bit of whitespace between the checkbox and the title.
97
98
  builder.text " " + title_for(options)
98
99
  end
100
+
101
+ if details = details_for(options)
102
+ builder.inline(:small, class: 'details') { builder.text details }
103
+ end
99
104
  end
100
105
  end
101
106
  end
@@ -117,14 +122,14 @@ module Trenni
117
122
  buffer << Builder.fragment do |builder|
118
123
  builder.inline(:dt) do
119
124
  builder.text title_for(options)
120
-
121
- if details = details_for(options)
122
- builder.inline(:small) { builder.text details }
123
- end
124
125
  end
125
126
 
126
127
  builder.tag(:dd) do
127
128
  klass.call(self, options, builder, &block)
129
+
130
+ if details = details_for(options)
131
+ builder.inline(:small, class: 'details') { builder.text details }
132
+ end
128
133
  end
129
134
  end
130
135
  end
@@ -24,11 +24,6 @@ module Trenni
24
24
  module Formatters
25
25
  module HTML
26
26
  module FormFormatter
27
- # The target object of the form.
28
- def object
29
- @object ||= @options[:object]
30
- end
31
-
32
27
  # Return true if the object is begin created or false if it is being updated.
33
28
  def new_record?
34
29
  object.new_record?
@@ -39,11 +34,6 @@ module Trenni
39
34
  options[:details]
40
35
  end
41
36
 
42
- # The name of the field, used for the name attribute of an input.
43
- def name_for(options)
44
- options[:name] || options[:field]
45
- end
46
-
47
37
  def field_for(options)
48
38
  options[:field]
49
39
  end
@@ -27,24 +27,24 @@ module Trenni
27
27
  def truncated_text(content, options = {})
28
28
  if content
29
29
  length = options.fetch(:length, 30)
30
-
30
+
31
31
  content = TruncatedText.truncate_text(content, length, options)
32
32
 
33
33
  return self.format(content)
34
34
  end
35
35
  end
36
-
36
+
37
37
  def self.truncate_text(text, truncate_at, options = {})
38
38
  return text.dup unless text.length > truncate_at
39
-
39
+
40
40
  options[:omission] ||= '...'
41
41
  length_with_room_for_omission = truncate_at - options[:omission].length
42
42
  stop = if options[:separator]
43
- text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
44
- else
45
- length_with_room_for_omission
46
- end
47
-
43
+ text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
44
+ else
45
+ length_with_room_for_omission
46
+ end
47
+
48
48
  "#{text[0...stop]}#{options[:omission]}"
49
49
  end
50
50
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Trenni
22
22
  module Formatters
23
- VERSION = "2.7.0"
23
+ VERSION = "2.8.0"
24
24
  end
25
25
  end
@@ -27,7 +27,7 @@ module Trenni::Formatters::FormFormatterSpec
27
27
  end
28
28
 
29
29
  describe Trenni::Formatters do
30
- let(:formatter) {FormFormatter.new(:object => double(:bar => 10))}
30
+ let(:formatter) {FormFormatter.for(double(bar: 10))}
31
31
 
32
32
  it "should generate form" do
33
33
  result = formatter.input(:field => :bar)
@@ -43,6 +43,27 @@ module Trenni::Formatters::FormFormatterSpec
43
43
  result = formatter.input(:field => :bar, :title => "Title")
44
44
  expect(result).to be == %Q{<dt>Title</dt>\n<dd><input name="bar" value="10"/></dd>}
45
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
46
67
  end
47
68
 
48
69
  describe "<input>" do
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency "trenni", "~> 3.4"
28
28
  spec.add_dependency "mapping", "~> 1.1"
29
29
 
30
- spec.add_development_dependency "bundler", "~> 1.3"
30
+ spec.add_development_dependency "bundler"
31
31
  spec.add_development_dependency "rspec", "~> 3.4"
32
32
  spec.add_development_dependency "rake"
33
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenni-formatters
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-03 00:00:00.000000000 Z
11
+ date: 2019-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trenni
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.3'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -133,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.6.10
136
+ rubygems_version: 3.0.1
138
137
  signing_key:
139
138
  specification_version: 4
140
139
  summary: Formatters for Trenni, to assist with typical views and form based interfaces.