diff --git a/.travis.yml b/.travis.yml index 6263408..9ad2c4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,8 @@ script: "bundle exec rake spec" env: - CI=true rvm: - - 1.9.2 - 1.9.3 - 2.0.0 + - 2.1.4 gemfile: - Gemfile diff --git a/Gemfile.lock b/Gemfile.lock index 5244b26..da8815e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,36 +1,35 @@ GEM remote: https://rubygems.org/ specs: - actionmailer (3.2.13) - actionpack (= 3.2.13) - mail (~> 2.5.3) - actionpack (3.2.13) - activemodel (= 3.2.13) - activesupport (= 3.2.13) - builder (~> 3.0.0) + actionmailer (4.1.6) + actionpack (= 4.1.6) + actionview (= 4.1.6) + mail (~> 2.5, >= 2.5.4) + actionpack (4.1.6) + actionview (= 4.1.6) + activesupport (= 4.1.6) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + actionview (4.1.6) + activesupport (= 4.1.6) + builder (~> 3.1) erubis (~> 2.7.0) - journey (~> 1.0.4) - rack (~> 1.4.5) - rack-cache (~> 1.2) - rack-test (~> 0.6.1) - sprockets (~> 2.2.1) - activemodel (3.2.13) - activesupport (= 3.2.13) - builder (~> 3.0.0) - activerecord (3.2.13) - activemodel (= 3.2.13) - activesupport (= 3.2.13) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activeresource (3.2.13) - activemodel (= 3.2.13) - activesupport (= 3.2.13) - activesupport (3.2.13) - i18n (= 0.6.1) - multi_json (~> 1.0) + activemodel (4.1.6) + activesupport (= 4.1.6) + builder (~> 3.1) + activerecord (4.1.6) + activemodel (= 4.1.6) + activesupport (= 4.1.6) + arel (~> 5.0.0) + activesupport (4.1.6) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) allison (2.0.3) - arel (3.0.2) - builder (3.0.4) + arel (5.0.1.20140414130214) + builder (3.2.2) diff-lcs (1.1.3) echoe (4.6.3) allison (>= 2.0.3) @@ -41,40 +40,34 @@ GEM erubis (2.7.0) gemcutter (0.7.1) hike (1.2.3) - i18n (0.6.1) - journey (1.0.4) - json (1.8.0) + i18n (0.6.11) + json (1.8.1) json_pure (1.8.0) - mail (2.5.4) - mime-types (~> 1.16) - treetop (~> 1.4.8) - mime-types (1.23) - multi_json (1.7.7) - polyglot (0.3.3) - rack (1.4.5) - rack-cache (1.2) - rack (>= 0.4) - rack-ssl (1.3.3) - rack + mail (2.6.1) + mime-types (>= 1.16, < 3) + mime-types (2.4.3) + minitest (5.4.2) + multi_json (1.10.1) + rack (1.5.2) rack-test (0.6.2) rack (>= 1.0) - rails (3.2.13) - actionmailer (= 3.2.13) - actionpack (= 3.2.13) - activerecord (= 3.2.13) - activeresource (= 3.2.13) - activesupport (= 3.2.13) - bundler (~> 1.0) - railties (= 3.2.13) - railties (3.2.13) - actionpack (= 3.2.13) - activesupport (= 3.2.13) - rack-ssl (~> 1.3.2) + rails (4.1.6) + actionmailer (= 4.1.6) + actionpack (= 4.1.6) + actionview (= 4.1.6) + activemodel (= 4.1.6) + activerecord (= 4.1.6) + activesupport (= 4.1.6) + bundler (>= 1.3.0, < 2.0) + railties (= 4.1.6) + sprockets-rails (~> 2.0) + railties (4.1.6) + actionpack (= 4.1.6) + activesupport (= 4.1.6) rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) - rake (10.1.0) - rdoc (3.12.2) + thor (>= 0.18.1, < 2.0) + rake (10.3.2) + rdoc (4.1.2) json (~> 1.4) rspec (2.9.0) rspec-core (~> 2.9.0) @@ -91,17 +84,20 @@ GEM rspec (~> 2.9.0) rubyforge (2.0.4) json_pure (>= 1.1.7) - sprockets (2.2.2) + sprockets (2.12.2) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - thor (0.18.1) + sprockets-rails (2.2.0) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (>= 2.8, < 4.0) + thor (0.19.1) + thread_safe (0.3.4) tilt (1.4.1) - treetop (1.4.14) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.37) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby diff --git a/lib/app/helpers/form_helper.rb b/lib/app/helpers/form_helper.rb index 1ce76d8..3e14fc6 100644 --- a/lib/app/helpers/form_helper.rb +++ b/lib/app/helpers/form_helper.rb @@ -6,12 +6,10 @@ module FormHelper # Mehtod that generates datepicker input field inside a form def datepicker(object_name, method, options = {}, timepicker = false) - input_tag = JqueryDatepicker::InstanceTag.new(object_name, method, self, options.delete(:object)) - dp_options, tf_options = input_tag.split_options(options) - tf_options[:value] = input_tag.format_date(tf_options[:value], String.new(dp_options[:dateFormat])) if tf_options[:value] && !tf_options[:value].empty? && dp_options.has_key?(:dateFormat) - html = input_tag.to_input_field_tag("text", tf_options) + input_tag = JqueryDatepicker::InstanceTag.new(object_name, method, self, options.merge(type: "text")) + html = input_tag.render method = timepicker ? "datetimepicker" : "datepicker" - html += javascript_tag("jQuery(document).ready(function(){jQuery('##{input_tag.get_name_and_id(tf_options.stringify_keys)["id"]}').#{method}(#{dp_options.to_json})});") + html += javascript_tag("jQuery(document).ready(function(){jQuery('##{input_tag.get_id}').#{method}(#{input_tag.dp_options.to_json})});") html.html_safe end end @@ -28,16 +26,24 @@ def datetime_picker(method, options = {}) end end -class JqueryDatepicker::InstanceTag < ActionView::Helpers::InstanceTag +class JqueryDatepicker::InstanceTag < ActionView::Helpers::Tags::TextField FORMAT_REPLACEMENTES = { "yy" => "%Y", "mm" => "%m", "dd" => "%d", "d" => "%-d", "m" => "%-m", "y" => "%y", "M" => "%b"} + attr_reader :dp_options - # Extending ActionView::Helpers::InstanceTag module to make Rails build the name and id - # Just returns the options before generate the HTML in order to use the same id and name (see to_input_field_tag mehtod) + # Intialize a TextField without all the DatePicker attributes and simple store the DatePicker attributes + def initialize(object_name, method_name, template_object, options = {}) + dp_options, tf_options = split_options(options) + tf_options[:value] = format_date(tf_options[:value], String.new(dp_options[:dateFormat])) if tf_options[:value] && !tf_options[:value].empty? && dp_options.has_key?(:dateFormat) + super(object_name, method_name, template_object, tf_options) + @dp_options = dp_options + end - def get_name_and_id(options = {}) + # Tags::TextField render already calls add_default_name_and_id but doesn't modify the instance @options, so call it here to ensure we calculate the same id attribute + def get_id + options = @options.stringify_keys add_default_name_and_id(options) - options + options['id'] end def available_datepicker_options @@ -62,4 +68,4 @@ def translate_format(format) format.gsub!(/#{FORMAT_REPLACEMENTES.keys.join("|")}/) { |match| FORMAT_REPLACEMENTES[match] } end -end \ No newline at end of file +end diff --git a/spec/jquery_datepicker_spec.rb b/spec/jquery_datepicker_spec.rb index b891835..796b129 100644 --- a/spec/jquery_datepicker_spec.rb +++ b/spec/jquery_datepicker_spec.rb @@ -13,27 +13,27 @@ describe JqueryDatepicker do let :valid_nested_response_input do - "" + "" end let :valid_nested_response_javascript do - "" + "" end let :valid_response_input do - "" + "" end let :valid_response_javascript do - "" + "" end let :valid_response_javascript_with_tf_options do - "" + "" end let :valid_response_javascript_datetime do - "" + "" end describe JqueryDatepicker::DatepickerHelper, :type => :view do @@ -111,47 +111,47 @@ end let :valid_response_javascript_with_options do - "" + "" end let :valid_response_javascript_with_options_id_and_name do - "" + "" end let :valid_response_javascript_with_options_M do - "" + "" end let :valid_response_javascript_with_options_dmY do - "" + "" end let :valid_response_input_with_options do - "" + "" end let :valid_response_input_with_options_id_and_name do - "" + "" end let :valid_response_input_with_options_empty do - "" + "" end let :valid_response_input_with_value do - "" + "" end let :valid_response_input_with_value_formatted do - "" + "" end let :valid_response_input_with_value_formatted_M do - "" + "" end let :valid_response_input_with_value_formatted_dmY do - "" + "" end it "should return a valid code when calling from the helper" do @@ -286,4 +286,4 @@ input_tag.translate_format("yy/m-dd").should eq("%Y/%-m-%d") end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74b03de..e2bee86 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ require 'active_record' require 'active_support/deprecation' require 'action_view' +require 'minitest/autorun' require 'rspec/rails/adapters' require 'rspec/rails/example/rails_example_group' require 'rspec/rails/matchers' @@ -30,4 +31,4 @@ def persisted? def att1 end -end \ No newline at end of file +end