commit 2a180fa43315edcc4d82ca896e88136a8f441f09 Author: John Bintz Date: Tue Oct 23 08:26:14 2012 -0400 initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..d8471b7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in formtastic-separate_html5_date_and_time.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..87b491e --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2012 John Bintz + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5fd802 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Formtastic::SeparateHtml5DateAndTime + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem 'formtastic-separate_html5_date_and_time' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install formtastic-separate_html5_date_and_time + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/formtastic-separate_html5_date_and_time.gemspec b/formtastic-separate_html5_date_and_time.gemspec new file mode 100644 index 0000000..8d5fd0d --- /dev/null +++ b/formtastic-separate_html5_date_and_time.gemspec @@ -0,0 +1,19 @@ +# -*- encoding: utf-8 -*- +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'formtastic-separate_html5_date_and_time/version' + +Gem::Specification.new do |gem| + gem.name = "formtastic-separate_html5_date_and_time" + gem.version = Formtastic::SeparateHtml5DateAndTime::VERSION + gem.authors = ["John Bintz"] + gem.email = ["john@coswellproductions.com"] + gem.description = %q{Separate HTML 5 date and time fields for use with existing polyfills and Mongoid} + gem.summary = %q{Separate HTML 5 date and time fields for use with existing polyfills and Mongoid} + gem.homepage = "" + + gem.files = `git ls-files`.split($/) + gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) + gem.require_paths = ["lib"] +end diff --git a/lib/formtastic-separate_html5_date_and_time.rb b/lib/formtastic-separate_html5_date_and_time.rb new file mode 100644 index 0000000..797b6f6 --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time.rb @@ -0,0 +1,17 @@ +require "formtastic-separate_html5_date_and_time/version" + +module Formtastic + module SeparateHtml5DateAndTime + class Railtie < ::Rails::Railtie + initializer 'separate_html5_date_and_time.initialize' do + require 'formtastic' + require 'formtastic-separate_html5_date_and_time/formtastic' + + if defined?(Mongoid::Document) + require 'formtastic-separate_html5_date_and_time/mongoid' + end + end + end + end +end + diff --git a/lib/formtastic-separate_html5_date_and_time/formtastic.rb b/lib/formtastic-separate_html5_date_and_time/formtastic.rb new file mode 100644 index 0000000..8c6ed09 --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time/formtastic.rb @@ -0,0 +1,31 @@ +class SeparateDateAndTimePickerInput < Formtastic::Inputs::DatetimePickerInput + def to_html + input_wrapping do + label_html << + builder.text_field("#{method}(date)", date_input_html_options) << + builder.text_field("#{method}(time)", time_input_html_options) + end + end + + def date_input_html_options + { 'type' => 'date', 'value' => date_value } + end + + + def time_input_html_options + { 'type' => 'time', 'value' => time_value } + end + + def date_value + value_parts.first + end + + def time_value + value_parts.last + end + + def value_parts + @value_parts ||= (value || '').split(' ') + end +end + diff --git a/lib/formtastic-separate_html5_date_and_time/mongoid.rb b/lib/formtastic-separate_html5_date_and_time/mongoid.rb new file mode 100644 index 0000000..1033271 --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time/mongoid.rb @@ -0,0 +1,28 @@ +require 'mongoid' + +module Mongoid + module ProcessSeparateDateAndTimePickerInputs + def process_attributes(attrs = nil, role = :default, guard_protected_attributes = true) + if attrs + datetime_attrs = {} + + attrs.each do |key, value| + if attr = key[/\A(.*)\((date|time)\)\Z/, 1] + datetime_attrs[attr] ||= {} + datetime_attrs[attr][key] = value + end + end + + datetime_attrs.each do |attr, values| + attrs[attr] = values["#{attr}(date)"] + ' ' + values["#{attr}(time)"] + + attrs.delete("#{attr}(date)") + attrs.delete("#{attr}(time)") + end + end + + super(attrs, role, guard_protected_attributes) + end + end +end + diff --git a/lib/formtastic-separate_html5_date_and_time/version.rb b/lib/formtastic-separate_html5_date_and_time/version.rb new file mode 100644 index 0000000..7646121 --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time/version.rb @@ -0,0 +1,5 @@ +module Formtastic + module SeparateHtml5DateAndTime + VERSION = "0.0.1" + end +end