From d7b60544960399fd589770364c3e81a866593324 Mon Sep 17 00:00:00 2001 From: neumayr Date: Thu, 17 Mar 2016 17:57:57 +0100 Subject: date_select helper with_css_classes option also accept a hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `date_select` helper `:with_css_classes` option now accepts a hash of strings for `:year`, `:month`, `:day`, `:hour`, `:minute`, `:second` that will extend the select type with the given css class value. ```erb <%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" } %> ``` ```html ``` Optional, add global `html_options` to modify every select tag in the set. ```erb <%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" }, { class: "my-date optional" } %> ``` Supported DateHelper methods: `select_day`, `select_month`, `select_year`, `select_hour`, `select_minute`, `select_second`, `select_datetime`, `select_time`, `time_select`, `date_select` and `datetime_select`. `:with_css_classes` option was added to the `date_select` with #7975. --- actionview/lib/action_view/helpers/date_helper.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/helpers/date_helper.rb b/actionview/lib/action_view/helpers/date_helper.rb index 233e613e97..43de922f74 100644 --- a/actionview/lib/action_view/helpers/date_helper.rb +++ b/actionview/lib/action_view/helpers/date_helper.rb @@ -226,8 +226,10 @@ module ActionView # for :year, :month, :day, :hour, :minute and :second. # Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds) # or the given prompt string. - # * :with_css_classes - Set to true if you want assign different styles for 'select' tags. This option - # automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags. + # * :with_css_classes - Set to true or a hash of strings. Use true if you want assign generic styles for + # select tags. This automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second'. A hash of + # strings for :year, :month, :day, :hour, :minute, :second + # will extend the select type with the given value. Use +html_options+ to modify every select tag in the set. # * :use_hidden - Set to true if you only want to generate hidden input tags. # # If anything is passed in the +html_options+ hash it will be applied to every select tag in the set. @@ -994,7 +996,7 @@ module ActionView :name => input_name_from_type(type) }.merge!(@html_options) select_options[:disabled] = 'disabled' if @options[:disabled] - select_options[:class] = [select_options[:class], type].compact.join(' ') if @options[:with_css_classes] + select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes] select_html = "\n" select_html << content_tag("option".freeze, '', :value => '') + "\n" if @options[:include_blank] @@ -1004,6 +1006,20 @@ module ActionView (content_tag("select".freeze, select_html.html_safe, select_options) + "\n").html_safe end + # Builds the css class value for the select element + # css_class_attribute(:year, 'date optional', with_css_classes: { year: 'my-year' }) + # => "date optional my-year" + def css_class_attribute(type, html_options_class, options) + css_class = case options + when Hash + options[type.to_sym] + else + type + end + + [html_options_class, css_class].compact.join(' ') + end + # Builds a prompt option tag with supplied options or from default options. # prompt_option_tag(:month, prompt: 'Select month') # => "" -- cgit v1.2.3