Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
124 changes: 60 additions & 64 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
28 changes: 17 additions & 11 deletions lib/app/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -62,4 +68,4 @@ def translate_format(format)
format.gsub!(/#{FORMAT_REPLACEMENTES.keys.join("|")}/) { |match| FORMAT_REPLACEMENTES[match] }
end

end
end
36 changes: 18 additions & 18 deletions spec/jquery_datepicker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
describe JqueryDatepicker do

let :valid_nested_response_input do
"<input id=\"foo_var_att1\" name=\"foo[var][att1]\" size=\"30\" type=\"text\" />"
"<input id=\"foo_var_att1\" name=\"foo[var][att1]\" type=\"text\" />"
end

let :valid_nested_response_javascript do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_var_att1').datepicker({})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_var_att1').datepicker({})});\n//]]>\n</script>"
end

let :valid_response_input do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" type=\"text\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" type=\"text\" />"
end

let :valid_response_javascript do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({})});\n//]]>\n</script>"
end

let :valid_response_javascript_with_tf_options do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#custom_id').datepicker({})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#custom_id').datepicker({})});\n//]]>\n</script>"
end

let :valid_response_javascript_datetime do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datetimepicker({})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datetimepicker({})});\n//]]>\n</script>"
end

describe JqueryDatepicker::DatepickerHelper, :type => :view do
Expand Down Expand Up @@ -111,47 +111,47 @@
end

let :valid_response_javascript_with_options do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"yy-mm-dd\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"yy-mm-dd\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
end

let :valid_response_javascript_with_options_id_and_name do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#custom_id').datepicker({\"dateFormat\":\"yy-mm-dd\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#custom_id').datepicker({\"dateFormat\":\"yy-mm-dd\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
end

let :valid_response_javascript_with_options_M do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"dd M yy\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"dd M yy\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
end

let :valid_response_javascript_with_options_dmY do
"<script type=\"text/javascript\">\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"m/d/y\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
"<script>\n//<![CDATA[\njQuery(document).ready(function(){jQuery('#foo_att1').datepicker({\"dateFormat\":\"m/d/y\",\"maxDate\":\"+1M +10D\",\"minDate\":-20})});\n//]]>\n</script>"
end

let :valid_response_input_with_options do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" tabindex=\"70\" type=\"text\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" tabindex=\"70\" type=\"text\" />"
end

let :valid_response_input_with_options_id_and_name do
"<input id=\"custom_id\" name=\"custom_name\" size=\"30\" tabindex=\"70\" type=\"text\" />"
"<input id=\"custom_id\" name=\"custom_name\" tabindex=\"70\" type=\"text\" />"
end

let :valid_response_input_with_options_empty do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" tabindex=\"70\" type=\"text\" value=\"\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" tabindex=\"70\" type=\"text\" value=\"\" />"
end

let :valid_response_input_with_value do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" type=\"text\" value=\"#{current_value.to_s}\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" type=\"text\" value=\"#{current_value.to_s}\" />"
end

let :valid_response_input_with_value_formatted do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" type=\"text\" value=\"#{current_value.strftime('%Y-%m-%d')}\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" type=\"text\" value=\"#{current_value.strftime('%Y-%m-%d')}\" />"
end

let :valid_response_input_with_value_formatted_M do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" type=\"text\" value=\"#{current_value.strftime('%d %b %Y')}\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" type=\"text\" value=\"#{current_value.strftime('%d %b %Y')}\" />"
end

let :valid_response_input_with_value_formatted_dmY do
"<input id=\"foo_att1\" name=\"foo[att1]\" size=\"30\" type=\"text\" value=\"#{current_value.strftime('%-m/%-d/%y')}\" />"
"<input id=\"foo_att1\" name=\"foo[att1]\" type=\"text\" value=\"#{current_value.strftime('%-m/%-d/%y')}\" />"
end

it "should return a valid code when calling from the helper" do
Expand Down Expand Up @@ -286,4 +286,4 @@
input_tag.translate_format("yy/m-dd").should eq("%Y/%-m-%d")
end
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -30,4 +31,4 @@ def persisted?
def att1

end
end
end