padrino-helpers 0.12.8.1 → 0.12.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +13 -5
  2. data/Rakefile +1 -5
  3. data/lib/padrino-helpers.rb +2 -1
  4. data/lib/padrino-helpers/asset_tag_helpers.rb +16 -4
  5. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +39 -1
  6. data/lib/padrino-helpers/form_builder/standard_form_builder.rb +1 -1
  7. data/lib/padrino-helpers/form_helpers.rb +220 -1
  8. data/lib/padrino-helpers/form_helpers/options.rb +7 -5
  9. data/lib/padrino-helpers/output_helpers.rb +1 -1
  10. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +1 -1
  11. data/lib/padrino-helpers/output_helpers/erb_handler.rb +2 -1
  12. data/lib/padrino-helpers/render_helpers.rb +23 -2
  13. data/lib/padrino-helpers/tag_helpers.rb +13 -0
  14. data/lib/padrino/rendering.rb +2 -1
  15. data/lib/padrino/rendering/erb_template.rb +12 -0
  16. data/lib/padrino/rendering/erubis_template.rb +1 -1
  17. data/padrino-helpers.gemspec +1 -1
  18. data/test/fixtures/markup_app/views/form_for.erb +28 -0
  19. data/test/fixtures/markup_app/views/form_for.haml +22 -0
  20. data/test/fixtures/markup_app/views/form_for.slim +21 -0
  21. data/test/fixtures/markup_app/views/form_tag.erb +21 -0
  22. data/test/fixtures/markup_app/views/form_tag.haml +14 -0
  23. data/test/fixtures/markup_app/views/form_tag.slim +14 -0
  24. data/test/helper.rb +14 -68
  25. data/test/test_asset_tag_helpers.rb +106 -93
  26. data/test/test_form_builder.rb +691 -450
  27. data/test/test_form_helpers.rb +770 -457
  28. data/test/test_format_helpers.rb +17 -37
  29. data/test/test_helpers.rb +8 -0
  30. data/test/test_output_helpers.rb +72 -72
  31. data/test/test_render_helpers.rb +142 -100
  32. data/test/test_rendering.rb +30 -6
  33. data/test/test_tag_helpers.rb +41 -39
  34. metadata +37 -23
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 008eb5f6eb8cd941fc5498e1ed53985dd84d3de4
4
- data.tar.gz: 65daa6ac52444c12c328e95bad7b56ba0133aa32
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWZmYWM3OThmOGFlNjFhMTZhMzUxNWFjYjBkNGQyZjYxOTI2NTM3Yg==
5
+ data.tar.gz: !binary |-
6
+ OWUzZmRkZGE3NTlkZTY4OWY5OGEyN2I3YzQ2YWE5NDAwZTUwOTQxNw==
5
7
  SHA512:
6
- metadata.gz: 466682d38365cd8e3c5cbb048fade5cde1c09f1de749c2e922f40ed47786e442433cdea68ee5e09c7d4411152f70d3a330ae32b2e7ca20415e5ccd6c86b184d6
7
- data.tar.gz: 3e23b26193925ffd0002f60091b5b041302a675ea9946ca997ecbb9ac1f0579843e80ffaa645fd2ca217ca5693be1419e2948563acc910602c934804eae24355
8
+ metadata.gz: !binary |-
9
+ YzBkY2FiOGQzNmEwNTA2N2NjMWQzZDIwY2Y4ZjU1OWJhOWI3NGFjZTVhZDhi
10
+ OTQxYjIyMGQ2MDZhYzdlZmY3MDQxZDBiYmE5NWI0MjUzZjgzNDUwZjg4NzIw
11
+ OTI4M2ZmYWYzMDhhMjlhY2Y4OWMyMDQ0Y2VmMTczYjQ1NDQ0ZDM=
12
+ data.tar.gz: !binary |-
13
+ YmYyOWE2NzFlZmNjYjJmMmJiZjM5NzFiMDg5ZTQ2NDljYzUzZDY2MjlhMGE0
14
+ NDNlMmZkZjVlZWQwN2ZmNzg5NzQ4YjgwZjdkZTlhMDVkZDE0ZTU2NjQ0YzAz
15
+ ZTVkZjA0MWI3NjMyZTE4NGE0NTBkYjM3ZGE4YTFkODUyZDVhYmQ=
data/Rakefile CHANGED
@@ -1,5 +1 @@
1
- # coding:utf-8
2
- RAKE_ROOT = __FILE__
3
-
4
- require 'rubygems'
5
- require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
@@ -41,8 +41,9 @@ module Padrino
41
41
  # end
42
42
  #
43
43
  def registered(app)
44
+ require 'padrino/rendering'
44
45
  app.register Padrino::Rendering
45
- app.set :default_builder, 'StandardFormBuilder'
46
+ app.set :default_builder, 'StandardFormBuilder' unless app.respond_to?(:default_builder)
46
47
  included(app)
47
48
  end
48
49
 
@@ -75,14 +75,14 @@ module Padrino
75
75
  # condition padrino return true/false if the request.path_info match the given url.
76
76
  #
77
77
  def link_to(*args, &block)
78
- options = args.extract_options!
78
+ options = args.extract_options!
79
79
  name = block_given? ? '' : args.shift
80
80
  href = args.first
81
81
  if fragment = options[:fragment] || options[:anchor]
82
82
  warn 'Options :anchor and :fragment are deprecated for #link_to. Please use :fragment for #url'
83
83
  href << '#' << fragment.to_s
84
84
  end
85
- options.reverse_merge!(:href => href || '#')
85
+ options = { :href => href ? escape_link(href) : '#' }.update(options)
86
86
  return name unless parse_conditions(href, options)
87
87
  block_given? ? content_tag(:a, options, &block) : content_tag(:a, name, options)
88
88
  end
@@ -204,11 +204,23 @@ module Padrino
204
204
  #
205
205
  def image_tag(url, options={})
206
206
  options.reverse_merge!(:src => image_path(url))
207
+ options[:alt] ||= image_alt(url) unless url =~ /\A(?:cid|data):/ || url.blank?
207
208
  tag(:img, options)
208
209
  end
209
210
 
210
211
  ##
211
- # Returns an html script tag for each of the sources provided.
212
+ # Returns a string suitable for an alt attribute of img element.
213
+ #
214
+ # @param [String] src
215
+ # The source path for the image tag.
216
+ # @return [String] The alt attribute value.
217
+ #
218
+ def image_alt(src)
219
+ File.basename(src, '.*').sub(/-[[:xdigit:]]{32,64}\z/, '').tr('-_', ' ').capitalize
220
+ end
221
+
222
+ ##
223
+ # Returns a html link tag for each of the sources provided.
212
224
  # You can pass in the filename without extension or a symbol and we search it in your +appname.public_folder+
213
225
  # like app/public/stylesheets for inclusion. You can provide also a full path.
214
226
  #
@@ -298,7 +310,7 @@ module Padrino
298
310
  #
299
311
  def asset_path(kind, source = nil)
300
312
  kind, source = source, kind if source.nil?
301
- source = asset_normalize_extension(kind, URI.escape(source.to_s))
313
+ source = asset_normalize_extension(kind, escape_link(source.to_s))
302
314
  return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//
303
315
  source = File.join(asset_folder_name(kind), source)
304
316
  timestamp = asset_timestamp(source)
@@ -125,6 +125,34 @@ module Padrino
125
125
  @template.image_submit_tag source, options
126
126
  end
127
127
 
128
+ def datetime_field(field, options={})
129
+ @template.datetime_field_tag field_name(field), default_options(field, options)
130
+ end
131
+
132
+ def datetime_local_field(field, options={})
133
+ @template.datetime_local_field_tag field_name(field), default_options(field, options)
134
+ end
135
+
136
+ def date_field(field, options={})
137
+ @template.date_field_tag field_name(field), default_options(field, options)
138
+ end
139
+
140
+ def month_field(field, options={})
141
+ @template.month_field_tag field_name(field), default_options(field, options)
142
+ end
143
+
144
+ def week_field(field, options={})
145
+ @template.week_field_tag field_name(field), default_options(field, options)
146
+ end
147
+
148
+ def time_field(field, options={})
149
+ @template.time_field_tag field_name(field), default_options(field, options)
150
+ end
151
+
152
+ def color_field(field, options={})
153
+ @template.color_field_tag field_name(field), default_options(field, options)
154
+ end
155
+
128
156
  ##
129
157
  # Supports nested fields for a child model within a form.
130
158
  # f.fields_for :addresses
@@ -151,7 +179,10 @@ module Padrino
151
179
 
152
180
  # Returns the known field types for a Formbuilder.
153
181
  def self.field_types
154
- [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
182
+ [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select,
183
+ :number_field, :telephone_field, :email_field, :search_field, :url_field,
184
+ :datetime_field, :datetime_local_field, :date_field, :month_field, :week_field, :time_field, :color_field,
185
+ ]
155
186
  end
156
187
 
157
188
  ##
@@ -161,6 +192,13 @@ module Padrino
161
192
  I18n.translate("#{object_model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :scope => :models)
162
193
  end
163
194
 
195
+ ##
196
+ # Returns the object's models name.
197
+ #
198
+ def object_model_name(explicit_object=object)
199
+ explicit_object.is_a?(Symbol) ? explicit_object : explicit_object.class.to_s.underscore.gsub(/\//, '_')
200
+ end
201
+
164
202
  ##
165
203
  # Returns the name for the given field.
166
204
  # field_name(:username) => "user[username]"
@@ -17,7 +17,7 @@ module Padrino
17
17
  (self.field_types - [ :hidden_field, :radio_button ]).each do |field_type|
18
18
  class_eval <<-EOF
19
19
  def #{field_type}_block(field, options={}, label_options={})
20
- label_options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
20
+ label_options = { :caption => options[:caption] }.update(label_options) if options[:caption]
21
21
  field_html = label(field, label_options)
22
22
  field_html << #{field_type}(field, options)
23
23
  @template.content_tag(:p, field_html)
@@ -92,7 +92,7 @@ module Padrino
92
92
  #
93
93
  def form_tag(url, options={}, &block)
94
94
  options = {
95
- :action => url,
95
+ :action => escape_link(url),
96
96
  :protect_from_csrf => is_protected_from_csrf?,
97
97
  'accept-charset' => 'UTF-8'
98
98
  }.update(options)
@@ -617,6 +617,185 @@ module Padrino
617
617
  input_tag(:range, options)
618
618
  end
619
619
 
620
+ DATETIME_ATTRIBUTES = [:value, :max, :min].freeze
621
+ COLOR_CODE_REGEXP = /\A#([0-9a-fA-F]{3}){1,2}\z/.freeze
622
+
623
+ ##
624
+ # Constructs a datetime tag from the given options.
625
+ #
626
+ # @example
627
+ # datetime_field_tag('datetime_with_min_max', :min => DateTime.new(1993, 2, 24, 12, 30, 45),
628
+ # :max => DateTime.new(2000, 4, 1, 12, 0, 0))
629
+ # datetime_field_tag('datetime_with_value', :value => DateTime.new(2000, 4, 1, 12, 0, 0))
630
+ #
631
+ # @param [String] name
632
+ # The name of the datetime field.
633
+ # @param [Hash] options
634
+ # The html options for the datetime field.
635
+ # @option options [DateTime, String] :min
636
+ # The min date time of the datetime field.
637
+ # @option options [DateTime, String] :max
638
+ # The max date time of the datetime field.
639
+ # @option options [DateTime, String] :value
640
+ # The value of the datetime field. See examples for details.
641
+ # @return [String] The html datetime field
642
+ #
643
+ def datetime_field_tag(name, options = {})
644
+ options = { :name => name }.update(options)
645
+ options = convert_attributes_into_datetime("%Y-%m-%dT%T.%L%z", options)
646
+ input_tag(:datetime, options)
647
+ end
648
+
649
+ ##
650
+ # Constructs a datetime-local tag from the given options.
651
+ #
652
+ # @example
653
+ # datetime_local_field_tag('datetime_local_with_min_max', :min => DateTime.new(1993, 2, 24, 12, 30, 45),
654
+ # :max => DateTime.new(2000, 4, 1, 12, 0, 0))
655
+ # datetime_local_field_tag('datetime_local_with_value', :value => DateTime.new(2000, 4, 1, 12, 0, 0))
656
+ #
657
+ # @param [String] name
658
+ # The name of the datetime local field.
659
+ # @param [Hash] options
660
+ # The html options for the datetime-local field.
661
+ # @option options [DateTime, String] :min
662
+ # The min date time of the datetime-local field.
663
+ # @option options [DateTime, String] :max
664
+ # The max date time of the datetime-local field.
665
+ # @option options [DateTime, String] :value
666
+ # The value of the datetime field. See examples for details.
667
+ # @return [String] The html datetime-local field
668
+ #
669
+ def datetime_local_field_tag(name, options = {})
670
+ options = { :name => name }.update(options)
671
+ options = convert_attributes_into_datetime("%Y-%m-%dT%T", options)
672
+ input_tag(:"datetime-local", options)
673
+ end
674
+
675
+ ##
676
+ # Constructs a date tag from the given options.
677
+ #
678
+ # @example
679
+ # date_field_tag('date_with_min_max', :min => DateTime.new(1993, 2, 24),
680
+ # :max => DateTime.new(2000, 4, 1))
681
+ # date_field_tag('date_with_value', :value => DateTime.new(2000, 4, 1))
682
+ #
683
+ # @param [String] name
684
+ # The name of the date field.
685
+ # @param [Hash] options
686
+ # The html options for the date field.
687
+ # @option options [DateTime, String] :min
688
+ # The min date time of the date field.
689
+ # @option options [DateTime, String] :max
690
+ # The max date time of the date field.
691
+ # @option options [DateTime, String] :value
692
+ # The value of the date field. See examples for details.
693
+ # @return [String] The html date field
694
+ #
695
+ def date_field_tag(name, options = {})
696
+ options = { :name => name }.update(options)
697
+ options = convert_attributes_into_datetime("%Y-%m-%d", options)
698
+ input_tag(:date, options)
699
+ end
700
+
701
+ ##
702
+ # Constructs a month tag from the given options.
703
+ #
704
+ # @example
705
+ # month_field_tag('month_with_min_max', :min => DateTime.new(1993, 2, 24),
706
+ # :max => DateTime.new(2000, 4, 1))
707
+ # month_field_tag('month_with_value', :value => DateTime.new(2000, 4, 1))
708
+ #
709
+ # @param [String] name
710
+ # The name of the month field.
711
+ # @param [Hash] options
712
+ # The html options for the month field.
713
+ # @option options [DateTime, String] :min
714
+ # The min month time of the month field.
715
+ # @option options [DateTime, String] :max
716
+ # The max month time of the month field.
717
+ # @option options [DateTime, String] :value
718
+ # The value of the month field. See examples for details.
719
+ # @return [String] The html month field
720
+ #
721
+ def month_field_tag(name, options = {})
722
+ options = { :name => name }.update(options)
723
+ options = convert_attributes_into_datetime("%Y-%m", options)
724
+ input_tag(:month, options)
725
+ end
726
+
727
+ ##
728
+ # Constructs a week tag from the given options.
729
+ #
730
+ # @example
731
+ # week_field_tag('week_with_min_max', :min => DateTime.new(1993, 2, 24),
732
+ # :max => DateTime.new(2000, 4, 1))
733
+ # week_field_tag('week_with_value', :value => DateTime.new(2000, 4, 1))
734
+ #
735
+ # @param [String] name
736
+ # The name of the week field.
737
+ # @param [Hash] options
738
+ # The html options for the week field.
739
+ # @option options [DateTime, String] :min
740
+ # The min week time of the week field.
741
+ # @option options [DateTime, String] :max
742
+ # The max week time of the week field.
743
+ # @option options [DateTime, String] :value
744
+ # The value of the week field. See examples for details.
745
+ # @return [String] The html week field
746
+ #
747
+ def week_field_tag(name, options = {})
748
+ options = { :name => name }.update(options)
749
+ options = convert_attributes_into_datetime("%Y-W%W", options)
750
+ input_tag(:week, options)
751
+ end
752
+
753
+ ##
754
+ # Constructs a time tag from the given options.
755
+ #
756
+ # @example
757
+ # time_field_tag('time_with_min_max', :max => Time.new(1993, 2, 24, 1, 19, 12),
758
+ # :min => Time.new(2008, 6, 21, 13, 30, 0))
759
+ # time_field_tag('time_with_value', :value => Time.new(2008, 6, 21, 13, 30, 0))
760
+ #
761
+ # @param [String] name
762
+ # The name of the time field.
763
+ # @param [Hash] options
764
+ # The html options for the time field.
765
+ # @option options [Time, DateTime, String] :min
766
+ # The min time of the time field.
767
+ # @option options [Time, DateTime, String] :max
768
+ # The max time of the time field.
769
+ # @option options [Time, DateTime, String] :value
770
+ # The value of the time field. See examples for details.
771
+ # @return [String] The html time field
772
+ #
773
+ def time_field_tag(name, options = {})
774
+ options = { :name => name }.update(options)
775
+ options = convert_attributes_into_datetime("%T.%L", options)
776
+ input_tag(:time, options)
777
+ end
778
+
779
+ ##
780
+ # Constructs a color tag from the given options.
781
+ #
782
+ # @example
783
+ # color_field_tag('color', :value => "#ff0000")
784
+ # color_field_tag('color', :value => "#f00")
785
+ #
786
+ # @param [String] name
787
+ # The name of the color field.
788
+ # @param [Hash] options
789
+ # The html options for the color field.
790
+ # @option options [String] :value
791
+ # The value of the color field. See examples for details.
792
+ #
793
+ def color_field_tag(name, options = {})
794
+ options = { :name => name }.update(options)
795
+ options[:value] = adjust_color(options[:value])
796
+ input_tag(:color, options)
797
+ end
798
+
620
799
  private
621
800
 
622
801
  ##
@@ -631,6 +810,46 @@ module Padrino
631
810
  builder_class = "Padrino::Helpers::FormBuilder::#{builder_class}".constantize if builder_class.is_a?(String)
632
811
  builder_class.new(self, object, options)
633
812
  end
813
+
814
+ ##
815
+ # Converts value into DateTime.
816
+ #
817
+ # @example
818
+ # datetime_value('1993-02-24T12:30:45') #=> #<DateTime: 1993-02-24T12:30:45+00:00>
819
+ #
820
+ def datetime_value(value)
821
+ if value.kind_of?(String)
822
+ DateTime.parse(value) rescue nil
823
+ else
824
+ value
825
+ end
826
+ end
827
+
828
+ ##
829
+ # Converts special attributes into datetime format strings that conforms to RFC 3399.
830
+ #
831
+ def convert_attributes_into_datetime(format, options)
832
+ DATETIME_ATTRIBUTES.each_with_object(options) do |attribute|
833
+ value = datetime_value(options[attribute])
834
+ options[attribute] = value.strftime(format) if value.respond_to?(:strftime)
835
+ end
836
+ end
837
+
838
+ ##
839
+ # Adjusts color code for the given color.
840
+ #
841
+ # @example
842
+ # adust_color("#000") #=> "#000000"
843
+ # adust_color("#ff0000") #=> "#ff0000"
844
+ # adust_color("#foobar") #=> "#000000"
845
+ #
846
+ def adjust_color(color)
847
+ return "#000000" unless color =~ COLOR_CODE_REGEXP
848
+ return color if (color_size = color.size) == 7
849
+ color.slice(1, color_size - 1).each_char.with_object("#") do |chr, obj|
850
+ obj << chr * 2
851
+ end
852
+ end
634
853
  end
635
854
  end
636
855
  end
@@ -45,7 +45,9 @@ module Padrino
45
45
  #
46
46
  def option_is_selected?(value, caption, selected_values)
47
47
  Array(selected_values).any? do |selected|
48
- [value.to_s, caption.to_s].include?(selected.to_s)
48
+ value ?
49
+ value.to_s == selected.to_s :
50
+ caption.to_s == selected.to_s
49
51
  end
50
52
  end
51
53
 
@@ -55,9 +57,9 @@ module Padrino
55
57
  def options_for_select(option_items, state = {})
56
58
  return [] if option_items.blank?
57
59
  option_items.map do |caption, value, attributes|
58
- html_attributes = { :value => value ||= caption }.merge(attributes||{})
59
- html_attributes[:selected] ||= option_is_selected?(value, caption, state[:selected])
60
- html_attributes[:disabled] ||= option_is_selected?(value, caption, state[:disabled])
60
+ html_attributes = { :value => value || caption }.merge(attributes||{})
61
+ html_attributes[:selected] ||= option_is_selected?(html_attributes[:value], caption, state[:selected])
62
+ html_attributes[:disabled] ||= option_is_selected?(html_attributes[:value], caption, state[:disabled])
61
63
  content_tag(:option, caption, html_attributes)
62
64
  end
63
65
  end
@@ -90,7 +92,7 @@ module Padrino
90
92
  collection.map{ |item| [ item.send(fields.first), item.send(fields.last) ] }
91
93
  else
92
94
  options.delete(:options) || []
93
- end
95
+ end
94
96
  end
95
97
  end
96
98
  end
@@ -80,7 +80,7 @@ module Padrino
80
80
  #
81
81
  def concat_content(text="")
82
82
  if handler = find_proper_handler
83
- handler.concat_to_template(text)
83
+ handler.concat_to_template(text, binding)
84
84
  else
85
85
  text
86
86
  end
@@ -46,7 +46,7 @@ module Padrino
46
46
  # @example
47
47
  # @handler.concat_to_template("This will be output to the template buffer")
48
48
  #
49
- def concat_to_template(text="")
49
+ def concat_to_template(text="", context=nil)
50
50
  text
51
51
  end
52
52