diff options
Diffstat (limited to 'actionpack/lib')
4 files changed, 43 insertions, 0 deletions
| diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 6510610034..c5a9057698 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -939,6 +939,15 @@ module ActionView          Tags::RadioButton.new(object_name, method, self, tag_value, options).render        end +      # Returns a text_field of type "color". +      # +      #   color_field("car", "color") +      #   # => <input id="car_color" name="car[color]" type="color" value="#000000" /> +      # +      def color_field(object_name, method, options = {}) +        Tags::ColorField.new(object_name, method, self, options).render +      end +        # Returns an input of type "search" for accessing a specified attribute (identified by +method+) on an object        # assigned to the template (identified by +object_name+). Inputs of type "search" may be styled differently by        # some browsers. diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index e65b4e3e95..7da67cd2f9 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -524,6 +524,14 @@ module ActionView          output.safe_concat("</fieldset>")        end +      # Creates a text field of type "color". +      # +      # ==== Options +      # * Accepts the same options as text_field_tag. +      def color_field_tag(name, value = nil, options = {}) +        text_field_tag(name, value, options.stringify_keys.update("type" => "color")) +      end +        # Creates a text field of type "search".        #        # ==== Options diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb index 5cd77c8ec3..c56165c769 100644 --- a/actionpack/lib/action_view/helpers/tags.rb +++ b/actionpack/lib/action_view/helpers/tags.rb @@ -8,6 +8,7 @@ module ActionView        autoload :CollectionCheckBoxes        autoload :CollectionRadioButtons        autoload :CollectionSelect +      autoload :ColorField        autoload :DateField        autoload :DateSelect        autoload :DatetimeSelect diff --git a/actionpack/lib/action_view/helpers/tags/color_field.rb b/actionpack/lib/action_view/helpers/tags/color_field.rb new file mode 100644 index 0000000000..6f08f8483a --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/color_field.rb @@ -0,0 +1,25 @@ +module ActionView +  module Helpers +    module Tags +      class ColorField < TextField #:nodoc: +        def render +          options = @options.stringify_keys +          options["value"] = @options.fetch("value") { validate_color_string(value(object)) } +          @options = options +          super +        end + +        private + +          def validate_color_string(string) +            regex = /#[0-9a-fA-F]{6}/ +            if regex.match(string) +              string.downcase +            else +              "#000000" +            end +          end +      end +    end +  end +end | 
