split_datetime 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Michi Huber
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Split Datetime (for Rails)
2
+ Have a datetime attribute? Want to have a datepicker for the date and dropdown menus for the time?
3
+ This gem adds the necessary accessors to your model.
4
+
5
+ ## Example Usage
6
+ In your `Gemfile`:
7
+
8
+ ```ruby
9
+ gem "split_datetime"
10
+ ```
11
+
12
+ After bundling, assuming you have an Event model with a starts_at attribute, add this to your model:
13
+
14
+ ```ruby
15
+ class Event < ActiveRecord::Base
16
+ extend SplitDatetime::Accessors
17
+ accepts_split_datetime_for :starts_at
18
+ end
19
+ ```
20
+
21
+ In your view:
22
+
23
+ ```erb
24
+ <%= simple_form_for @event do |f| %>
25
+ <%= f.input :starts_at_date, as: :string, input_html: { class: 'datepicker' } %>
26
+ <%= f.input :starts_at_hour, collection: 0..24 %>
27
+ <%= f.input :starts_at_min, collection: [0, 15, 30, 45] %>
28
+ <%= ... %>
29
+ <% end %>
30
+ ```
31
+
32
+ Add your js datepicker and you're good to go. (Of course, this also works with standard rails form helpers).
33
+
34
+ ## Options
35
+ `starts_at` will be Time.now with the minute set to 0 by default. If you want to change this, pass in a lambda wrapping the default. E.g.:
36
+
37
+ ```ruby
38
+ accepts_split_datetime_for :starts_at, default: lambda { Time.now.change(min: 0) + 2.weeks }
39
+ accepts_split_datetime_for :starts_at, default: lambda { nil }
40
+ ```
41
+
42
+ You can also specify the date format for the view:
43
+
44
+ ```ruby
45
+ accepts_split_datetime_for :starts_at, format: "%D"
46
+ ```
47
+
48
+ See `Time#strftime` for formats. Default is `"%F"`.
@@ -0,0 +1 @@
1
+ require 'split_datetime/accessors'
@@ -0,0 +1,45 @@
1
+ module SplitDatetime
2
+ module Accessors
3
+ def accepts_split_datetime_for(*attrs)
4
+ opts = { format: "%F", default: lambda { Time.now.change(min: 0) } }
5
+
6
+ if attrs.last.class == Hash
7
+ custom = attrs.delete_at(-1)
8
+ opts = opts.merge(custom)
9
+ end
10
+
11
+ attrs.each do |attr|
12
+ attr_accessible "#{attr}_date", "#{attr}_hour", "#{attr}_min"
13
+
14
+ define_method(attr) do
15
+ super() || opts[:default].call
16
+ end
17
+
18
+ define_method("#{attr}_date=") do |date|
19
+ date = Date.parse(date.to_s)
20
+ self.send("#{attr}=", self.send(attr).change(year: date.year, month: date.month, day: date.day))
21
+ end
22
+
23
+ define_method("#{attr}_hour=") do |hour|
24
+ self.send("#{attr}=", self.send(attr).change(hour: hour, min: self.send(attr).min))
25
+ end
26
+
27
+ define_method("#{attr}_min=") do |min|
28
+ self.send("#{attr}=", self.send(attr).change(min: min))
29
+ end
30
+
31
+ define_method("#{attr}_date") do
32
+ self.send(attr).strftime(opts[:format])
33
+ end
34
+
35
+ define_method("#{attr}_hour") do
36
+ self.send(attr).hour
37
+ end
38
+
39
+ define_method("#{attr}_min") do
40
+ self.send(attr).min
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module SplitDatetime
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,79 @@
1
+ require 'active_support/all'
2
+ require_relative "../../lib/split_datetime/accessors.rb"
3
+
4
+ describe SplitDatetime::Accessors do
5
+ let(:model) { Model.new }
6
+ before do
7
+ class ModelParent; attr_accessor :starts_at; end
8
+ class Model < ModelParent; attr_accessor :starts_at; end
9
+ Model.extend(SplitDatetime::Accessors)
10
+ Model.should_receive(:attr_accessible).with("starts_at_date", "starts_at_hour", "starts_at_min")
11
+ Model.accepts_split_datetime_for(:starts_at)
12
+ end
13
+
14
+ describe "#starts_at" do
15
+ it "lets the superclass (usually ActiveRecord::Base) override the value" do
16
+ class ModelParent; def starts_at; 5; end; end
17
+ model.starts_at.should == 5
18
+ end
19
+
20
+ it "sets the default time to Time.now" do
21
+ now = Time.new(2222, 12, 22, 13, 44)
22
+ Time.stub(:now) { now }
23
+ model.starts_at.should == now.change(min: 0)
24
+ end
25
+
26
+ it "allows setting the default value through options" do
27
+ Model.stub(:attr_accessible)
28
+ Model.accepts_split_datetime_for(:starts_at, default: lambda { 10 })
29
+ Model.new.starts_at.should == 10
30
+ end
31
+ end
32
+
33
+ describe "split datetime methods" do
34
+ before { model.starts_at = Time.new(2222, 12, 22, 13, 44, 0) }
35
+ describe "#starts_at_date" do
36
+ it "returns the model's starts_at date as string" do
37
+ model.starts_at_date.should == "2222-12-22"
38
+ end
39
+
40
+ it "lets you modify the format" do
41
+ Model.stub(:attr_accessible)
42
+ Model.accepts_split_datetime_for(:starts_at, format: "%D")
43
+ model.starts_at_date.should == "12/22/22"
44
+ end
45
+
46
+ it "sets the appropiate parts of #starts_at" do
47
+ model.starts_at_date = Time.new(1111, 1, 1)
48
+ model.starts_at.should == Time.new(1111, 1, 1, 13, 44, 0)
49
+ end
50
+
51
+ it "can set from a string" do
52
+ model.starts_at_date = "1111-01-01"
53
+ model.starts_at.should == Time.new(1111, 1, 1, 13, 44, 0)
54
+ end
55
+ end
56
+
57
+ describe "#starts_at_hour" do
58
+ it "returns the hour" do
59
+ model.starts_at_hour.should == 13
60
+ end
61
+
62
+ it "sets the hour of starts_at" do
63
+ model.starts_at_hour = 11
64
+ model.starts_at.should == Time.new(2222, 12, 22, 11, 44, 0)
65
+ end
66
+ end
67
+
68
+ describe "#starts_at_min" do
69
+ it "returns the min" do
70
+ model.starts_at_min.should == 44
71
+ end
72
+
73
+ it "sets the minute of #starts_at" do
74
+ model.starts_at_min = 55
75
+ model.starts_at.should == Time.new(2222, 12, 22, 13, 55, 0)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'split_datetime/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "split_datetime"
6
+ s.version = SplitDatetime::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Michi Huber"]
9
+ s.email = ["michi.huber@gmail.com"]
10
+ s.homepage = "http://github.com/michihuber/split_datetime"
11
+ s.summary = "Split datetime inputs into text and dropdowns in rails views"
12
+ s.description = <<-END
13
+ Adds accessors to a class so that the date can be set as a string while minutes and hours can be set as integers. This allows you to have an input textfield (with a javascript datepicker) for the date and dropdowns for the time.
14
+ END
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- spec/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'rspec', '~> 2.11'
22
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: split_datetime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michi Huber
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70243475244500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70243475244500
25
+ description: ! ' Adds accessors to a class so that the date can be set as a string
26
+ while minutes and hours can be set as integers. This allows you to have an input
27
+ textfield (with a javascript datepicker) for the date and dropdowns for the time.
28
+
29
+ '
30
+ email:
31
+ - michi.huber@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE
37
+ - README.md
38
+ - lib/split_datetime.rb
39
+ - lib/split_datetime/accessors.rb
40
+ - lib/split_datetime/version.rb
41
+ - spec/split_datetime/accessors_spec.rb
42
+ - split_datetime.gemspec
43
+ homepage: http://github.com/michihuber/split_datetime
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.11
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Split datetime inputs into text and dropdowns in rails views
67
+ test_files:
68
+ - spec/split_datetime/accessors_spec.rb