trenni-formatters 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e2814d2a4811d2a066fd96b9cc0c9bc78f7dbe4
4
- data.tar.gz: 525e508d5b65000040c12b3c09b5452c2f899ef3
3
+ metadata.gz: ac162860960979c57091379978f2bacfb93ef759
4
+ data.tar.gz: 9c31ab0d5f6c847a97512dfd1513bd83c87b1124
5
5
  SHA512:
6
- metadata.gz: 5fbaeb44cb3ad9a3d6f3b499043f2a8e2703b03dd59b998349423384e32be1c927a89d2ec662f13edfe5eeb3f5e2621160231df9d32ae82aa506e0ab118a7c41
7
- data.tar.gz: 1fe8760cbe647bc9ab1266666bdde4bc6e6b771f86bfc874af30b29cab7d3b4f1815610612b3e9222c2f007434ff0d3fb84832b03b54395f20d54e524ac7d41b
6
+ metadata.gz: 0ca42bc7c0c37c207f9ed0db93696cde7c3c899788d3c0f8c7a0537bc43ebdc7045144fdee8b5336dfc12d364fb9b44069423c90a7fe9402f023656a3a10b895
7
+ data.tar.gz: d17162fb3e392f9001d2e6c95f46d9140211b8b28d6ebfcd7854e76237e36ca4234a35cd41a66a8dd92c5cd19306d4655c3e576af3d54667731d67da5616cd4b
@@ -27,13 +27,11 @@ module Trenni
27
27
  def initialize(options = {})
28
28
  @options = options
29
29
  end
30
+
31
+ attr :options
30
32
 
31
33
  def format_unspecified(object, options)
32
- if object
33
- object
34
- else
35
- options[:blank] || ""
36
- end
34
+ object.to_s
37
35
  end
38
36
 
39
37
  def format(object, options = {})
@@ -43,7 +41,7 @@ module Trenni
43
41
  self.send(method_name, object, options)
44
42
  else
45
43
  format_unspecified(object, options)
46
- end.to_s
44
+ end
47
45
  end
48
46
 
49
47
  alias text format
@@ -51,6 +49,20 @@ module Trenni
51
49
  def [] key
52
50
  @options[key]
53
51
  end
52
+
53
+ map(String) do |object, options|
54
+ object
55
+ end
56
+
57
+ map(NilClass) do |object, options|
58
+ options[:blank] || @options[:blank] || ""
59
+ end
60
+
61
+ [TrueClass, FalseClass, Fixnum, Bignum, Float, Rational].each do |klass|
62
+ map(klass) do |object, options|
63
+ object.to_s
64
+ end
65
+ end
54
66
  end
55
67
  end
56
68
  end
@@ -26,16 +26,12 @@ module Trenni
26
26
  module FormFormatter
27
27
  # The target object of the form.
28
28
  def object
29
- @options[:object]
29
+ @object ||= @options[:object]
30
30
  end
31
31
 
32
32
  # Return true if the object is begin created or false if it is being updated.
33
33
  def new_record?
34
- if object.respond_to? :new_record?
35
- return object.new_record?
36
- elsif self.object.respond_to? :saved?
37
- return !object.saved?
38
- end
34
+ object.new_record?
39
35
  end
40
36
 
41
37
  # Any additional details relating to a field (e.g. explanation text)
@@ -51,6 +51,14 @@ module Trenni
51
51
  end
52
52
  end
53
53
 
54
+ def raw_value_for(options)
55
+ @formatter.raw_value_for(options)
56
+ end
57
+
58
+ def raw_value
59
+ @raw_value ||= raw_value_for(@options)
60
+ end
61
+
54
62
  def value_for(options)
55
63
  @formatter.value_for(options)
56
64
  end
@@ -62,7 +70,7 @@ module Trenni
62
70
  def option_attributes_for(options)
63
71
  return {
64
72
  :value => value_for(options),
65
- :selected => options.fetch(:selected){ value_for(@options) == value_for(options) },
73
+ :selected => options.fetch(:selected){ raw_value == raw_value_for(options) },
66
74
  :id => options[:id],
67
75
  :class => options[:class],
68
76
  :data => options[:data],
@@ -44,6 +44,14 @@ module Trenni
44
44
  def name_for(options)
45
45
  @formatter.name_for(options)
46
46
  end
47
+
48
+ def raw_value_for(options)
49
+ @formatter.raw_value_for(options)
50
+ end
51
+
52
+ def raw_value
53
+ @raw_value ||= raw_value_for(@options)
54
+ end
47
55
 
48
56
  def value_for(options)
49
57
  @formatter.value_for(options)
@@ -59,7 +67,7 @@ module Trenni
59
67
  :name => @field,
60
68
  # We set a default value to empty string, otherwise it becomes "on".
61
69
  :value => value_for(options) || "",
62
- :checked => options.fetch(:selected){ value_for(@options) == value_for(options) },
70
+ :checked => options.fetch(:selected){ raw_value == raw_value_for(options) },
63
71
  :data => options[:data],
64
72
  }
65
73
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Trenni
22
22
  module Formatters
23
- VERSION = "2.2.0"
23
+ VERSION = "2.3.0"
24
24
  end
25
25
  end
@@ -99,6 +99,8 @@ module Trenni::Formatters::FormFormatterSpec
99
99
  end
100
100
 
101
101
  it "should escape characters correctly" do
102
+ expect(formatter.object).to receive(:new_record?).and_return(false)
103
+
102
104
  result = formatter.submit
103
105
  expect(result).to be == %Q{<input type="submit" value="Update"/>}
104
106
  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.2.0
4
+ version: 2.3.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-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trenni
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.5.2
137
+ rubygems_version: 2.6.10
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: Formatters for Trenni, to assist with typical views and form based interfaces.