sequent-sinatra 0.1.4 → 0.1.5

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: ff3009b374b8b75cad6de45571b76487c92a691d
4
- data.tar.gz: 8fa9e461c0c80a3f7b2610c8af6eeb915287165e
3
+ metadata.gz: 97c87c781855a940c9c66574a9f68342d159ea66
4
+ data.tar.gz: 287e0f1e73c596ef6a814df83831bf8f6ce13e8d
5
5
  SHA512:
6
- metadata.gz: eecf5a4b6e328bad1282f225248d01b1a7c239cae5b55e6b917ae4feb824845b234283af491765cfec2bece2b85c63eca2d7611f61cb9cc0a875dcff05eae175
7
- data.tar.gz: 4ce04e459bde7fe61d229495795e2595b135b67821dfda16a426fa0c4c50f71b57796419679f2faee1a03c226380103351002a28bd9f73fad4ade20fa6fc2951
6
+ metadata.gz: 00a7d067aeb7a22595443ffd3303f6ca8f57cb3d7d1941d2ff5289f064c3c8b44a09da57372274b9310c862291c04e86cda0d9d9a2bad5c7a10ce35f44da846c
7
+ data.tar.gz: 59a215973ffc08a03604ad3981ba4b430e4f416c662c7d1900e4ece04b58a0227ddf1b590fcae4c48ad13469e000d1d611d24c3d5144f2e82f93c5e3726e9aa3
@@ -13,6 +13,10 @@ module Sequent
13
13
  nil
14
14
  end
15
15
 
16
+ def postfix_for_id
17
+ nil
18
+ end
19
+
16
20
  def initialize(parent, path, params, errors, options = {})
17
21
  raise "params are empty while creating new fieldset path #{path}" unless params
18
22
  @values = params[path] || {}
@@ -22,9 +26,10 @@ module Sequent
22
26
  @options = options
23
27
  end
24
28
 
25
- def nested_array(name)
26
- (@values[name] || [{}]).each do |value|
27
- yield FieldsetInArray.new(self, name, { name => value }, @errors, @options)
29
+ def nested_array(name, default_if_empty = {})
30
+ values = @values[name].present? ? @values[name] : [default_if_empty].compact
31
+ values.each_with_index do |value, index|
32
+ yield FieldsetInArray.new(self, name, { name => value }, @errors, @options.merge(index_in_array: index))
28
33
  end
29
34
  end
30
35
 
@@ -36,15 +41,16 @@ module Sequent
36
41
  @parent.send(method, *args)
37
42
  end
38
43
 
39
- def path_for(field_name)
40
- css_id @path, field_name
41
- end
42
44
  end
43
45
 
44
46
  class FieldsetInArray < Fieldset
45
47
  def postfix
46
48
  "[]"
47
49
  end
50
+
51
+ def postfix_for_id
52
+ @options[:index_in_array].to_s
53
+ end
48
54
  end
49
55
  end
50
56
  end
@@ -17,10 +17,6 @@ module Sequent
17
17
  @options = options
18
18
  end
19
19
 
20
- def path_for(field_name)
21
- css_id field_name
22
- end
23
-
24
20
  def method_missing(method, *args, &block)
25
21
  @context.send(method, *args)
26
22
  end
@@ -14,11 +14,12 @@ module Sequent
14
14
  # :value - the default checked value if the current object has none
15
15
  # :class - the css class
16
16
  def raw_checkbox(field, options={})
17
- id = css_id(@path, field)
17
+ id = calculate_id(field)
18
18
  value = param_or_default(field, options[:value]) || id
19
19
  values = [value].compact
20
20
  single_tag :input, options.merge(
21
- :type => "checkbox", :id => id,
21
+ :type => "checkbox",
22
+ :id => id,
22
23
  :name => calculate_name(field),
23
24
  :value => value, checked: (values.include?(@values[field.to_s])) ? "checked" : nil
24
25
  )
@@ -56,12 +57,12 @@ module Sequent
56
57
  # +options+ Hash with optional attributes.
57
58
  # :value - the default value if the current object has none
58
59
  # :class - the css class
59
- # :rows - the number of rows of the textarea
60
+ # :rows - the number of rows of the textarea, default 3
60
61
  def raw_textarea(field, options={})
61
62
  value = param_or_default(field, options[:value])
62
63
 
63
64
  with_closing_tag :textarea, value, {rows: "3"}.merge(options.merge(
64
- :id => css_id(@path, field),
65
+ :id => calculate_id(field),
65
66
  :name => calculate_name(field)
66
67
  ))
67
68
  end
@@ -97,39 +98,44 @@ module Sequent
97
98
  option_values.merge!(disabled: "disabled") if options[:disable].try(:include?, id)
98
99
  content << tag(:option, text, option_values)
99
100
  end
100
- tag :select, content, options.merge(:id => css_id(@path, field), :name => calculate_name(field))
101
+ tag :select, content, options.merge(id: calculate_id(field), name: calculate_name(field))
101
102
  end
102
103
 
104
+ def full_path(field)
105
+ tree_in_names(field, :postfix_for_id).join('_')
106
+ end
107
+
108
+ alias_method :calculate_id, :full_path
109
+
103
110
  def calculate_name(field)
104
- reverse_names = tree_in_names(field)
111
+ reverse_names = tree_in_names(field, :postfix)
105
112
  "#{reverse_names.first}#{reverse_names[1..-1].map { |n| n == '[]' ? n : "[#{n}]" }.join}"
106
113
  end
107
114
 
108
- def full_path(field)
109
- tree_in_names(field).join('_')
115
+ def param_or_default(field, default)
116
+ @values.nil? ? default : @values.has_key?(field.to_s) ? @values[field.to_s] || default : default
110
117
  end
111
118
 
112
- alias_method :calculate_id, :full_path
119
+ def merge_and_append_class_attributes(to_append, options = {})
120
+ to_append.merge(options) do |key, oldval, newval|
121
+ key == :class ? "#{oldval} #{newval}" : newval
122
+ end
123
+ end
113
124
 
114
- def tree_in_names(field)
115
- if respond_to? :path
116
- names = [field, postfix, path].compact
117
- parent = @parent
118
- while parent.is_a? Fieldset
119
- names << parent.postfix if parent.postfix
120
- names << parent.path
121
- parent = parent.parent
122
- end
123
- names.reverse
125
+ def i18n_name(field)
126
+ if @path
127
+ "#{@path}.#{field}"
124
128
  else
125
- [field]
129
+ field.to_s
126
130
  end
127
131
  end
128
132
 
129
- def param_or_default(field, default)
130
- @values.nil? ? default : @values.has_key?(field.to_s) ? @values[field.to_s] || default : default
133
+ def has_form_error?(name)
134
+ @errors.try(:has_key?, name.to_sym)
131
135
  end
132
136
 
137
+ private
138
+
133
139
  def id_and_text_from_value(val)
134
140
  if val.is_a? Array
135
141
  [val[0], val[1]]
@@ -138,24 +144,12 @@ module Sequent
138
144
  end
139
145
  end
140
146
 
141
- def css_id(*things)
142
- things.compact.map { |t| t.to_s }.join('_').downcase.gsub(/\W/, '_')
143
- end
144
-
145
147
  def tag(name, content, options={})
146
148
  "<#{name.to_s}" +
147
149
  (options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') +
148
150
  (content.nil? ? '>' : ">#{content}</#{name}>")
149
151
  end
150
152
 
151
- def single_tag(name, options={})
152
- "<#{name.to_s} #{hash_to_html_attrs(options)} />"
153
- end
154
-
155
- def with_closing_tag(name, value, options={})
156
- %Q{<#{name.to_s} #{hash_to_html_attrs(options)} >#{h value}</#{name.to_s}>}
157
- end
158
-
159
153
  def hash_to_html_attrs(options={})
160
154
  raise %Q{Keys used in options must be a Symbol. Don't use {"class" => "col-md-4"} but use {class: "col-md-4"}} if options.keys.find { |k| not k.kind_of? Symbol }
161
155
  html_attrs = ""
@@ -166,32 +160,15 @@ module Sequent
166
160
  html_attrs.chop
167
161
  end
168
162
 
169
- def merge_and_append_class_attributes(to_append, options = {})
170
- to_append.merge(options) do |key, oldval, newval|
171
- key == :class ? "#{oldval} #{newval}" : newval
172
- end
173
- end
174
-
175
- def i18n_name(field)
176
- if @path
177
- "#{@path}.#{field}"
178
- else
179
- field.to_s
180
- end
181
- end
182
-
183
- def has_form_error?(name)
184
- @errors.try(:has_key?, name.to_sym)
185
- end
186
163
 
187
- private
188
164
  def raw_field(field, field_type, options)
189
165
  value = param_or_default(field, options[:value])
190
166
  if options[:formatter]
191
167
  value = self.send(options[:formatter], value)
192
168
  options.delete(:formatter)
193
169
  end
194
- id = options[:id] || css_id(@path, field)
170
+ #TODO use calculate_id
171
+ id = options[:id] || calculate_id(field)
195
172
  single_tag :input, options.merge(
196
173
  :type => field_type,
197
174
  :id => id,
@@ -200,6 +177,28 @@ module Sequent
200
177
  )
201
178
  end
202
179
 
180
+ def tree_in_names(field, postfix_method_name)
181
+ if respond_to? :path
182
+ names = [field, send(postfix_method_name), path].compact
183
+ parent = @parent
184
+ while parent.is_a? Fieldset
185
+ names << parent.postfix if parent.send(postfix_method_name)
186
+ names << parent.path
187
+ parent = parent.parent
188
+ end
189
+ names.reverse
190
+ else
191
+ [field]
192
+ end
193
+ end
194
+
195
+ def single_tag(name, options={})
196
+ "<#{name.to_s} #{hash_to_html_attrs(options)} />"
197
+ end
198
+
199
+ def with_closing_tag(name, value, options={})
200
+ %Q{<#{name.to_s} #{hash_to_html_attrs(options)} >#{h value}</#{name.to_s}>}
201
+ end
203
202
 
204
203
  end
205
204
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SequentSinatra
2
- VERSION='0.1.4'
2
+ VERSION='0.1.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequent-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Vonk