Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaning up to prepare for ruby-forge release

Got rid of all dos line endings (ugh.. I hate those things)
Refactored directory structure to get ready for Hoe transition
Rake helper to set the version (in javascript and in ruby)
  • Loading branch information...
commit f31736821bf21dfeaf46ccaaa87e2373fda10644 1 parent 6ec4ecb
@timcharper authored
View
1  .gitignore
@@ -0,0 +1 @@
+pkg
View
18 CHANGELOG → History.txt
@@ -1,3 +1,16 @@
+== Version 1.11.1 ==
+ * properly wrap buttons if they are too big for the line
+ * more translations: german (Jonas), Added russian translation (DEkart)
+ * locale fixes:
+ * Fix: time doesn't work (using 12 hour instead of 24 hour) in Italian format.
+ * updated Portuguese translation with the "Clear" action (Daniel Luz)
+ * Portuguese was missing month of October
+ * Added a clear button (Hendy Tanata)
+ * Reverted a change that attempted to fix position in a scrollable div, but caused probles elsewhere
+ * Added :minute_interval to calendar_date_select_process_options, fixing http://code.google.com/p/calendardateselect/issues/detail?id=81
+ * Add helpers to give the list of javascripts and stylesheets (calendar_date_select_javascripts and calendar_date_select_stylesheets)
+ * Converted over to use Hoe (echoe was giving too many problems). Refactored the directory structure to make it more gem-ish.
+
== Version 1.11 ==
* Calendar Date Select now works as a ruby-gem (thanks, artmotion!)
@@ -197,4 +210,7 @@ June 2, 2007
==Version 1.0==
June 1, 2007
- * Initial release!
+ * Initial release!
+
+
+
View
39 Manifest.txt
@@ -0,0 +1,39 @@
+History.txt
+init.rb
+js_test/functional/cds_test.html
+js_test/prototype.js
+js_test/test.css
+js_test/unit/cds_helper_methods.html
+js_test/unittest.js
+lib/calendar_date_select/calendar_date_select.rb
+lib/calendar_date_select/includes_helper.rb
+lib/calendar_date_select.rb
+Manifest.txt
+MIT-LICENSE
+public/blank_iframe.html
+public/images/calendar_date_select/calendar.gif
+public/javascripts/calendar_date_select/calendar_date_select.js
+public/javascripts/calendar_date_select/format_american.js
+public/javascripts/calendar_date_select/format_db.js
+public/javascripts/calendar_date_select/format_euro_24hr.js
+public/javascripts/calendar_date_select/format_euro_24hr_ymd.js
+public/javascripts/calendar_date_select/format_finnish.js
+public/javascripts/calendar_date_select/format_hyphen_ampm.js
+public/javascripts/calendar_date_select/format_iso_date.js
+public/javascripts/calendar_date_select/format_italian.js
+public/javascripts/calendar_date_select/locale/de.js
+public/javascripts/calendar_date_select/locale/fi.js
+public/javascripts/calendar_date_select/locale/fr.js
+public/javascripts/calendar_date_select/locale/pl.js
+public/javascripts/calendar_date_select/locale/pt.js
+public/javascripts/calendar_date_select/locale/ru.js
+public/stylesheets/calendar_date_select/blue.css
+public/stylesheets/calendar_date_select/default.css
+public/stylesheets/calendar_date_select/plain.css
+public/stylesheets/calendar_date_select/red.css
+public/stylesheets/calendar_date_select/silver.css
+Rakefile
+Readme.txt
+test/functional/calendar_date_select_test.rb
+test/functional/helper_methods_test.rb
+test/test_helper.rb
View
51 Rakefile
@@ -1,24 +1,31 @@
-require 'echoe'
-# try "rake -T" so see the wunderfull tasks generated by the little code below ;-)
+# -*- ruby -*-
-Echoe.new("timcharper-calendar_date_select") do |p|
- p.version = "1.11.1"
- p.author = "Tim Harper"
- p.email = "timcharper@gmail.com"
- p.url = "http://github.com/timcharper/calendar_date_select"
- p.summary = "A popular and flexible JavaScript DatePicker for RubyOnRails"
- p.description = <<-END_OF_DESCRIPTION
- CalendarDateSelect is semi-light-weight and easy to use!
- It takes full advantage of the prototype.js library, resulting in less code,
- but maintaining a great deal of functionality.
- Project site: http://code.google.com/p/calendardateselect/
- END_OF_DESCRIPTION
- p.has_rdoc = false
- p.platform = Gem::Platform::RUBY
- p.ignore_pattern = Dir.glob("{tmp}/**/*")
- p.test_files = [
- "test/test_helper.rb",
- "test/functional/calendar_date_select_test.rb",
- "test/functional/helper_methods_test.rb"
- ]
+require 'rubygems'
+require 'hoe'
+
+$: << File.dirname(__FILE__) + "/lib/"
+require "activesupport"
+require './lib/calendar_date_select.rb'
+
+Hoe.new('calendar_date_select', CalendarDateSelect::VERSION) do |p|
+ p.rubyforge_name = 'calendar_date_select'
+ p.developer('Tim Harper', 'tim c harper at gmail dot com')
+end
+
+
+task :set_version do
+ ["lib/calendar_date_select/calendar_date_select.rb", "public/javascripts/calendar_date_select/calendar_date_select.js"].each do |file|
+ abs_file = File.dirname(__FILE__) + "/" + file
+ src = File.read(abs_file);
+ src = src.map do |line|
+ case line
+ when /^ *VERSION/ then " VERSION = '#{ENV['VERSION']}'\n"
+ when /^\/\/ CalendarDateSelect version / then "// CalendarDateSelect version #{ENV['VERSION']} - a prototype based date picker\n"
+ else
+ line
+ end
+ end.join
+ File.open(abs_file, "wb") { |f| f << src }
+ end
end
+# vim: syntax=Ruby
View
36 README.textile → Readme.txt
@@ -1,20 +1,16 @@
-h1. CalendarDateSelect
-
-Author: Tim Harper ( 'tim_see_harperATgmail._see_om'.gsub('_see_', 'c').gsub('AT', '@') )
-
-h2. Examples
-
-"See a demo here":http://electronicholas.com/calendar
-
-h2. Project Site
-
-http://code.google.com/p/calendardateselect/
-
-h2. Submitting patches
-
-Please take care to do the following:
-
-* Clean up your patch (don't send a patch bomb with a hundred features in one)
-* Write test cases!
-* As a general rule of thumb, think of ways to make things more general purpose than specific.
-
+= CalendarDateSelect
+
+http://code.google.com/p/calendardateselect/
+
+== Examples
+
+"See a demo here":http://electronicholas.com/calendar
+
+== Submitting patches
+
+Please take care to do the following:
+
+* Clean up your patch (don't send a patch bomb with a hundred features in one)
+* Write test cases!
+* As a general rule of thumb, think of ways to make things more general purpose than specific.
+
View
33 calendar_date_select.gemspec
@@ -0,0 +1,33 @@
+Gem::Specification.new do |s|
+ s.name = %q{calendar_date_select}
+ s.version = "1.11.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Tim Harper"]
+ s.date = %q{2008-11-23}
+ s.description = %q{}
+ s.email = ["tim c harper at gmail dot com"]
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "Readme.txt"]
+ s.files = ["History.txt", "init.rb", "js_test/functional/cds_test.html", "js_test/prototype.js", "js_test/test.css", "js_test/unit/cds_helper_methods.html", "js_test/unittest.js", "lib/calendar_date_select/calendar_date_select.rb", "lib/calendar_date_select/includes_helper.rb", "lib/calendar_date_select.rb", "Manifest.txt", "MIT-LICENSE", "public/blank_iframe.html", "public/images/calendar_date_select/calendar.gif", "public/javascripts/calendar_date_select/calendar_date_select.js", "public/javascripts/calendar_date_select/format_american.js", "public/javascripts/calendar_date_select/format_db.js", "public/javascripts/calendar_date_select/format_euro_24hr.js", "public/javascripts/calendar_date_select/format_euro_24hr_ymd.js", "public/javascripts/calendar_date_select/format_finnish.js", "public/javascripts/calendar_date_select/format_hyphen_ampm.js", "public/javascripts/calendar_date_select/format_iso_date.js", "public/javascripts/calendar_date_select/format_italian.js", "public/javascripts/calendar_date_select/locale/de.js", "public/javascripts/calendar_date_select/locale/fi.js", "public/javascripts/calendar_date_select/locale/fr.js", "public/javascripts/calendar_date_select/locale/pl.js", "public/javascripts/calendar_date_select/locale/pt.js", "public/javascripts/calendar_date_select/locale/ru.js", "public/stylesheets/calendar_date_select/blue.css", "public/stylesheets/calendar_date_select/default.css", "public/stylesheets/calendar_date_select/plain.css", "public/stylesheets/calendar_date_select/red.css", "public/stylesheets/calendar_date_select/silver.css", "Rakefile", "Readme.txt", "test/functional/calendar_date_select_test.rb", "test/functional/helper_methods_test.rb", "test/test_helper.rb"]
+ s.has_rdoc = true
+ s.homepage = %q{http://code.google.com/p/calendardateselect/}
+ s.rdoc_options = ["--main", "README.txt"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{calendar_date_select}
+ s.rubygems_version = %q{1.3.0}
+ s.summary = %q{}
+ s.test_files = ["test/test_helper.rb"]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
+ else
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
+ end
+ else
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
+ end
+end
View
18 init.rb
@@ -1,17 +1 @@
-%w[calendar_date_select includes_helper].each { |file|
- require File.join( File.dirname(__FILE__), "lib",file)
-}
-
-ActionView::Helpers::FormHelper.send(:include, CalendarDateSelect::FormHelper)
-ActionView::Base.send(:include, CalendarDateSelect::FormHelper)
-ActionView::Base.send(:include, CalendarDateSelect::IncludesHelper)
-
-# install files
-unless File.exists?(RAILS_ROOT + '/public/javascripts/calendar_date_select/calendar_date_select.js')
- ['/public', '/public/javascripts/calendar_date_select', '/public/stylesheets/calendar_date_select', '/public/images/calendar_date_select', '/public/javascripts/calendar_date_select/locale'].each do |dir|
- source = File.join(File.dirname(__FILE__), dir)
- dest = RAILS_ROOT + dir
- FileUtils.mkdir_p(dest)
- FileUtils.cp(Dir.glob(source+'/*.*'), dest)
- end
-end
+require File.dirname(__FILE__) + "/lib/calendar_date_select.rb"
View
241 lib/calendar_date_select.rb
@@ -1,222 +1,19 @@
-class CalendarDateSelect
- FORMATS = {
- :natural => {
- :date => "%B %d, %Y",
- :time => " %I:%M %p"
- },
- :hyphen_ampm => {
- :date => "%Y-%m-%d",
- :time => " %I:%M %p",
- :javascript_include => "format_hyphen_ampm"
- },
- :iso_date => {
- :date => "%Y-%m-%d",
- :time => " %H:%M",
- :javascript_include => "format_iso_date"
- },
- :finnish => {
- :date => "%d.%m.%Y",
- :time => " %H:%M",
- :javascript_include => "format_finnish"
- },
- :american => {
- :date => "%m/%d/%Y",
- :time => " %I:%M %p",
- :javascript_include => "format_american"
- },
- :euro_24hr => {
- :date => "%d %B %Y",
- :time => " %H:%M",
- :javascript_include => "format_euro_24hr"
- },
- :euro_24hr_ymd => {
- :date => "%Y.%m.%d",
- :time => " %H:%M",
- :javascript_include => "format_euro_24hr_ymd"
- },
- :italian => {
- :date => "%d/%m/%Y",
- :time => " %H:%M",
- :javascript_include => "format_italian"
- },
- :db => {
- :date => "%Y-%m-%d",
- :time => "%H:%M",
- :javascript_include => "format_db"
- }
- }
-
- cattr_accessor :image
- @@image = "calendar_date_select/calendar.gif"
-
- cattr_reader :format
- @@format = FORMATS[:natural]
-
- class << self
- def format=(format)
- raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
- @@format = FORMATS[format]
- end
-
- def javascript_format_include
- @@format[:javascript_include] && "calendar_date_select/#{@@format[:javascript_include]}"
- end
-
- def date_format_string(time=false)
- @@format[:date] + ( time ? @@format[:time] : "" )
- end
-
- def format_date(date)
- if Date===date
- date.strftime(date_format_string(false))
- else
- date.strftime(date_format_string(true))
- end
- end
-
- def has_time?(value)
- /[0-9]:[0-9]{2}/.match(value.to_s)
- end
- end
-
- module FormHelper
- def calendar_date_select_tag( name, value = nil, options = {})
- calendar_options = calendar_date_select_process_options(options)
- value = format_time(value, calendar_options)
-
- calendar_options.delete(:format)
-
- options[:id] ||= name
- tag = calendar_options[:hidden] || calendar_options[:embedded] ?
- hidden_field_tag(name, value, options) :
- text_field_tag(name, value, options)
-
- calendar_date_select_output(tag, calendar_options)
- end
-
- def format_time(value, options = {})
- if value.respond_to?("strftime")
- if options[:format]
- value = value.strftime(options[:format])
- else
- if options.has_key? :time
- value = value.strftime(CalendarDateSelect.date_format_string(options[:time]))
- else
- value = CalendarDateSelect.format_date(value)
- end
- end
- end
- value
- end
-
- # extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
- def calendar_date_select_process_options(options)
- calendar_options = {}
- callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
- for key in [:time, :valid_date_check, :embedded, :buttons, :clear_button, :format, :year_range, :month_year, :popup, :hidden, :minute_interval] + callbacks
- calendar_options[key] = options.delete(key) if options.has_key?(key)
- end
-
- # if passing in mixed, pad it with single quotes
- calendar_options[:time] = "'mixed'" if calendar_options[:time].to_s=="mixed"
- calendar_options[:month_year] = "'#{calendar_options[:month_year]}'" if calendar_options[:month_year]
-
- # if we are forcing the popup, automatically set the readonly property on the input control.
- if calendar_options[:popup].to_s == "force"
- calendar_options[:popup] = "'force'"
- options[:readonly] = true
- end
-
- if (vdc=calendar_options.delete(:valid_date_check))
- if vdc.include?(";") || vdc.include?("function")
- throw ":valid_date_check function is missing a 'return' statement. Try something like: :valid_date_check => 'if (date > new(Date)) return true; else return false;'" unless vdc.include?("return");
- end
-
- vdc = "return(#{vdc})" unless vdc.include?("return")
- vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
- calendar_options[:valid_date_check] = vdc
- end
-
- calendar_options[:popup_by] ||= "this" if calendar_options[:hidden]
-
- # surround any callbacks with a function, if not already done so
- for key in callbacks
- calendar_options[key] = "function(param) { #{calendar_options[key]} }" unless calendar_options[key].include?("function") if calendar_options[key]
- end
-
- calendar_options[:year_range] = format_year_range(calendar_options[:year_range] || 10)
- calendar_options
- end
-
- def calendar_date_select(object, method, options={})
- obj = options[:object] || instance_variable_get("@#{object}")
-
- if !options.include?(:time) && obj.class.respond_to?("columns_hash")
- column_type = (obj.class.columns_hash[method.to_s].type rescue nil)
- options[:time] = true if column_type == :datetime
- end
-
- use_time = options[:time]
-
- if options[:time].to_s=="mixed"
- use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
- end
-
- calendar_options = calendar_date_select_process_options(options)
-
- options[:value] ||=
- if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
- obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
- elsif obj.respond_to?("#{method}_before_type_cast")
- obj.send("#{method}_before_type_cast")
- elsif obj.respond_to?(method)
- obj.send(method).to_s
- else
- nil
- end
-
- tag = ActionView::Helpers::InstanceTag.new(object, method, self, nil, options.delete(:object))
- calendar_date_select_output(
- tag.to_input_field_tag( (calendar_options[:hidden] || calendar_options[:embedded]) ? "hidden" : "text", options),
- calendar_options
- )
- end
-
- def calendar_date_select_output(input, calendar_options = {})
- out = input
- if calendar_options[:embedded]
- uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
- # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
- out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
-
- out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(calendar_options)} ); ")
- else
- out << " "
-
- out << image_tag(CalendarDateSelect.image,
- :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(calendar_options)} );",
- :style => 'border:0px; cursor:pointer;')
- end
-
- out
- end
-
- private
- def format_year_range(year) # nodoc
- return year unless year.respond_to?(:first)
- return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
- return "[#{year.first.year}, #{year.last.year}]"
- end
- end
-end
-
-
-module ActionView
- module Helpers
- class FormBuilder
- def calendar_date_select(method, options = {})
- @template.calendar_date_select(@object_name, method, options.merge(:object => @object))
- end
- end
- end
-end
+require "calendar_date_select/calendar_date_select.rb"
+require "calendar_date_select/includes_helper.rb"
+
+if Object.const_defined?(:Rails) && File.directory?(Rails.root + "/public")
+ ActionView::Helpers::FormHelper.send(:include, CalendarDateSelect::FormHelper)
+ ActionView::Base.send(:include, CalendarDateSelect::FormHelper)
+ ActionView::Base.send(:include, CalendarDateSelect::IncludesHelper)
+
+ # install files
+ unless File.exists?(RAILS_ROOT + '/public/javascripts/calendar_date_select/calendar_date_select.js')
+ ['/public', '/public/javascripts/calendar_date_select', '/public/stylesheets/calendar_date_select', '/public/images/calendar_date_select', '/public/javascripts/calendar_date_select/locale'].each do |dir|
+ source = File.dirname(__FILE__) + "/../#{dir}"
+ dest = RAILS_ROOT + dir
+ FileUtils.mkdir_p(dest)
+ FileUtils.cp(Dir.glob(source+'/*.*'), dest)
+ end
+ end
+
+end
View
223 lib/calendar_date_select/calendar_date_select.rb
@@ -0,0 +1,223 @@
+class CalendarDateSelect
+ VERSION = '1.11.1'
+ FORMATS = {
+ :natural => {
+ :date => "%B %d, %Y",
+ :time => " %I:%M %p"
+ },
+ :hyphen_ampm => {
+ :date => "%Y-%m-%d",
+ :time => " %I:%M %p",
+ :javascript_include => "format_hyphen_ampm"
+ },
+ :iso_date => {
+ :date => "%Y-%m-%d",
+ :time => " %H:%M",
+ :javascript_include => "format_iso_date"
+ },
+ :finnish => {
+ :date => "%d.%m.%Y",
+ :time => " %H:%M",
+ :javascript_include => "format_finnish"
+ },
+ :american => {
+ :date => "%m/%d/%Y",
+ :time => " %I:%M %p",
+ :javascript_include => "format_american"
+ },
+ :euro_24hr => {
+ :date => "%d %B %Y",
+ :time => " %H:%M",
+ :javascript_include => "format_euro_24hr"
+ },
+ :euro_24hr_ymd => {
+ :date => "%Y.%m.%d",
+ :time => " %H:%M",
+ :javascript_include => "format_euro_24hr_ymd"
+ },
+ :italian => {
+ :date => "%d/%m/%Y",
+ :time => " %H:%M",
+ :javascript_include => "format_italian"
+ },
+ :db => {
+ :date => "%Y-%m-%d",
+ :time => "%H:%M",
+ :javascript_include => "format_db"
+ }
+ }
+
+ cattr_accessor :image
+ @@image = "calendar_date_select/calendar.gif"
+
+ cattr_reader :format
+ @@format = FORMATS[:natural]
+
+ class << self
+ def format=(format)
+ raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
+ @@format = FORMATS[format]
+ end
+
+ def javascript_format_include
+ @@format[:javascript_include] && "calendar_date_select/#{@@format[:javascript_include]}"
+ end
+
+ def date_format_string(time=false)
+ @@format[:date] + ( time ? @@format[:time] : "" )
+ end
+
+ def format_date(date)
+ if Date===date
+ date.strftime(date_format_string(false))
+ else
+ date.strftime(date_format_string(true))
+ end
+ end
+
+ def has_time?(value)
+ /[0-9]:[0-9]{2}/.match(value.to_s)
+ end
+ end
+
+ module FormHelper
+ def calendar_date_select_tag( name, value = nil, options = {})
+ calendar_options = calendar_date_select_process_options(options)
+ value = format_time(value, calendar_options)
+
+ calendar_options.delete(:format)
+
+ options[:id] ||= name
+ tag = calendar_options[:hidden] || calendar_options[:embedded] ?
+ hidden_field_tag(name, value, options) :
+ text_field_tag(name, value, options)
+
+ calendar_date_select_output(tag, calendar_options)
+ end
+
+ def format_time(value, options = {})
+ if value.respond_to?("strftime")
+ if options[:format]
+ value = value.strftime(options[:format])
+ else
+ if options.has_key? :time
+ value = value.strftime(CalendarDateSelect.date_format_string(options[:time]))
+ else
+ value = CalendarDateSelect.format_date(value)
+ end
+ end
+ end
+ value
+ end
+
+ # extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
+ def calendar_date_select_process_options(options)
+ calendar_options = {}
+ callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
+ for key in [:time, :valid_date_check, :embedded, :buttons, :clear_button, :format, :year_range, :month_year, :popup, :hidden, :minute_interval] + callbacks
+ calendar_options[key] = options.delete(key) if options.has_key?(key)
+ end
+
+ # if passing in mixed, pad it with single quotes
+ calendar_options[:time] = "'mixed'" if calendar_options[:time].to_s=="mixed"
+ calendar_options[:month_year] = "'#{calendar_options[:month_year]}'" if calendar_options[:month_year]
+
+ # if we are forcing the popup, automatically set the readonly property on the input control.
+ if calendar_options[:popup].to_s == "force"
+ calendar_options[:popup] = "'force'"
+ options[:readonly] = true
+ end
+
+ if (vdc=calendar_options.delete(:valid_date_check))
+ if vdc.include?(";") || vdc.include?("function")
+ throw ":valid_date_check function is missing a 'return' statement. Try something like: :valid_date_check => 'if (date > new(Date)) return true; else return false;'" unless vdc.include?("return");
+ end
+
+ vdc = "return(#{vdc})" unless vdc.include?("return")
+ vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
+ calendar_options[:valid_date_check] = vdc
+ end
+
+ calendar_options[:popup_by] ||= "this" if calendar_options[:hidden]
+
+ # surround any callbacks with a function, if not already done so
+ for key in callbacks
+ calendar_options[key] = "function(param) { #{calendar_options[key]} }" unless calendar_options[key].include?("function") if calendar_options[key]
+ end
+
+ calendar_options[:year_range] = format_year_range(calendar_options[:year_range] || 10)
+ calendar_options
+ end
+
+ def calendar_date_select(object, method, options={})
+ obj = options[:object] || instance_variable_get("@#{object}")
+
+ if !options.include?(:time) && obj.class.respond_to?("columns_hash")
+ column_type = (obj.class.columns_hash[method.to_s].type rescue nil)
+ options[:time] = true if column_type == :datetime
+ end
+
+ use_time = options[:time]
+
+ if options[:time].to_s=="mixed"
+ use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
+ end
+
+ calendar_options = calendar_date_select_process_options(options)
+
+ options[:value] ||=
+ if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
+ obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
+ elsif obj.respond_to?("#{method}_before_type_cast")
+ obj.send("#{method}_before_type_cast")
+ elsif obj.respond_to?(method)
+ obj.send(method).to_s
+ else
+ nil
+ end
+
+ tag = ActionView::Helpers::InstanceTag.new(object, method, self, nil, options.delete(:object))
+ calendar_date_select_output(
+ tag.to_input_field_tag( (calendar_options[:hidden] || calendar_options[:embedded]) ? "hidden" : "text", options),
+ calendar_options
+ )
+ end
+
+ def calendar_date_select_output(input, calendar_options = {})
+ out = input
+ if calendar_options[:embedded]
+ uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
+ # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
+ out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
+
+ out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(calendar_options)} ); ")
+ else
+ out << " "
+
+ out << image_tag(CalendarDateSelect.image,
+ :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(calendar_options)} );",
+ :style => 'border:0px; cursor:pointer;')
+ end
+
+ out
+ end
+
+ private
+ def format_year_range(year) # nodoc
+ return year unless year.respond_to?(:first)
+ return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
+ return "[#{year.first.year}, #{year.last.year}]"
+ end
+ end
+end
+
+
+module ActionView
+ module Helpers
+ class FormBuilder
+ def calendar_date_select(method, options = {})
+ @template.calendar_date_select(@object_name, method, options.merge(:object => @object))
+ end
+ end
+ end
+end
View
76 lib/includes_helper.rb → lib/calendar_date_select/includes_helper.rb 100755 → 100644
@@ -1,38 +1,38 @@
-class CalendarDateSelect
- module IncludesHelper
- def calendar_date_select_stylesheets(options = {})
- options.assert_valid_keys(:style, :format, :locale)
-
- style = options[:style]
- cds_css_file = style ? "calendar_date_select/#{style}" : "calendar_date_select/default"
- return cds_css_file
- end
-
- def calendar_date_select_javascripts(options = {})
- options.assert_valid_keys(:style, :format, :locale)
-
- style = options[:style]
- locale = options[:locale]
- cds_css_file = style ? "calendar_date_select/#{style}" : "calendar_date_select/default"
-
- output = []
- output << "calendar_date_select/calendar_date_select"
- output << "calendar_date_select/locale/#{locale}" if locale
- output << CalendarDateSelect.javascript_format_include if CalendarDateSelect.javascript_format_include
- return output
- end
-
- def calendar_date_select_includes(*args)
- return "" if @cds_already_included
- @cds_already_included=true
-
- options = (Hash === args.last) ? args.pop : {}
- options.assert_valid_keys(:style, :format, :locale)
- options[:style] ||= args.shift
-
- js = javascript_include_tag(*calendar_date_select_javascripts(options))
- css = stylesheet_link_tag(*calendar_date_select_stylesheets(options))
- "#{js}\n#{css}\n"
- end
- end
-end
+class CalendarDateSelect
+ module IncludesHelper
+ def calendar_date_select_stylesheets(options = {})
+ options.assert_valid_keys(:style, :format, :locale)
+
+ style = options[:style]
+ cds_css_file = style ? "calendar_date_select/#{style}" : "calendar_date_select/default"
+ return cds_css_file
+ end
+
+ def calendar_date_select_javascripts(options = {})
+ options.assert_valid_keys(:style, :format, :locale)
+
+ style = options[:style]
+ locale = options[:locale]
+ cds_css_file = style ? "calendar_date_select/#{style}" : "calendar_date_select/default"
+
+ output = []
+ output << "calendar_date_select/calendar_date_select"
+ output << "calendar_date_select/locale/#{locale}" if locale
+ output << CalendarDateSelect.javascript_format_include if CalendarDateSelect.javascript_format_include
+ return output
+ end
+
+ def calendar_date_select_includes(*args)
+ return "" if @cds_already_included
+ @cds_already_included=true
+
+ options = (Hash === args.last) ? args.pop : {}
+ options.assert_valid_keys(:style, :format, :locale)
+ options[:style] ||= args.shift
+
+ js = javascript_include_tag(*calendar_date_select_javascripts(options))
+ css = stylesheet_link_tag(*calendar_date_select_stylesheets(options))
+ "#{js}\n#{css}\n"
+ end
+ end
+end
View
3  lib/gem_init.rb
@@ -1,3 +0,0 @@
-# delegate to original init.rb from the plugin
-require File.dirname(__FILE__) + '/../init'
-
View
70 public/javascripts/calendar_date_select/format_hyphen_ampm.js
@@ -1,36 +1,36 @@
-Date.prototype.toFormattedString = function(include_time){
- str = this.getFullYear() + "-" + Date.padded2(this.getMonth() + 1) + "-" +Date.padded2(this.getDate());
-
-if (include_time) { hour=this.getHours(); str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() + " " + this.getAMPM() }
-return str;
-}
-
-Date.parseFormattedString = function (string) {
- var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
- "( ([0-9]{1,2}):([0-9]{2})? *(pm|am)" +
- "?)?)?)?";
- var d = string.match(new RegExp(regexp, "i"));
- if (d==null) return Date.parse(string); // at least give javascript a crack at it.
- var offset = 0;
- var date = new Date(d[1], 0, 1);
- if (d[3]) { date.setMonth(d[3] - 1); }
- if (d[5]) { date.setDate(d[5]); }
- if (d[7]) {
- hours = parseInt(d[7], 10);
- offset=0;
- is_pm = (d[9].toLowerCase()=="pm")
- if (is_pm && hours <= 11) hours+=12;
- if (!is_pm && hours == 12) hours=0;
- date.setHours(hours);
-
- }
- if (d[8]) { date.setMinutes(d[8]); }
- if (d[10]) { date.setSeconds(d[10]); }
- if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
- if (d[14]) {
- offset = (Number(d[16]) * 60) + Number(d[17]);
- offset *= ((d[15] == '-') ? 1 : -1);
- }
-
- return date;
+Date.prototype.toFormattedString = function(include_time){
+ str = this.getFullYear() + "-" + Date.padded2(this.getMonth() + 1) + "-" +Date.padded2(this.getDate());
+
+if (include_time) { hour=this.getHours(); str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() + " " + this.getAMPM() }
+return str;
+}
+
+Date.parseFormattedString = function (string) {
+ var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
+ "( ([0-9]{1,2}):([0-9]{2})? *(pm|am)" +
+ "?)?)?)?";
+ var d = string.match(new RegExp(regexp, "i"));
+ if (d==null) return Date.parse(string); // at least give javascript a crack at it.
+ var offset = 0;
+ var date = new Date(d[1], 0, 1);
+ if (d[3]) { date.setMonth(d[3] - 1); }
+ if (d[5]) { date.setDate(d[5]); }
+ if (d[7]) {
+ hours = parseInt(d[7], 10);
+ offset=0;
+ is_pm = (d[9].toLowerCase()=="pm")
+ if (is_pm && hours <= 11) hours+=12;
+ if (!is_pm && hours == 12) hours=0;
+ date.setHours(hours);
+
+ }
+ if (d[8]) { date.setMinutes(d[8]); }
+ if (d[10]) { date.setSeconds(d[10]); }
+ if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
+ if (d[14]) {
+ offset = (Number(d[16]) * 60) + Number(d[17]);
+ offset *= ((d[15] == '-') ? 1 : -1);
+ }
+
+ return date;
}
View
11 test/test_helper.rb
@@ -9,15 +9,8 @@
require 'ostruct'
-for file in ["../lib/calendar_date_select.rb", "../lib/includes_helper.rb"]
- require File.expand_path(File.join(File.dirname(__FILE__), file))
-end
-
-def dbg
- require 'ruby-debug'
- Debugger.start
- debugger
-end
+$: << (File.dirname(__FILE__) + "/../lib")
+require "calendar_date_select"
class Object
def to_regexp
Please sign in to comment.
Something went wrong with that request. Please try again.