date_checkbox 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Matthias Viehweger
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,88 @@
1
+ = DateCheckbox
2
+
3
+ This is a rails-plugin which can create a checkbox for attributes which are
4
+ datetime-fields in the database. So, if you want to store the date when a user
5
+ appected some terms or received some goods, but want to circumvent the need of
6
+ selecting the proper date and time from 6 dropdowns, you can just use this
7
+ plugin.
8
+
9
+ It creates some wrapper-methods on the model for the attributes you specify and
10
+ provides a form-helper which uses these. The model-methods also work with
11
+ standard checkbox-tags.
12
+
13
+
14
+ Personally, I do not want to go through the hassle of selecting the proper date
15
+ and time from 6 dropdowns. I just want to check a checkbox and have my app to
16
+ the rest. In many cases, this is even required security- or permission-wise.
17
+
18
+ While this functionality is fairly easy to implement for one datetime-field
19
+ (like terms_accepted_at), I don't like to repeat this code.
20
+
21
+ Therefore, I put this rails-plugin together.
22
+
23
+ A neat side-effect is, that I went and created a form-helper and added some
24
+ useful model methods along the way.
25
+
26
+
27
+ == Installation
28
+
29
+ Just add
30
+
31
+ gem 'date_checkbox'
32
+
33
+ to your Gemfile. If your not using Bundler and Rails3 by now, we can still be
34
+ friends, but thats about it.
35
+
36
+
37
+ == Example
38
+
39
+ # app/models/user.rb
40
+ class User < ActiveRecord::Base
41
+ has_date_checkbox :terms_accepted_at
42
+ end
43
+
44
+ This adds the following three methods to the user-model:
45
+
46
+ * terms_accepted # returns "0" or "1"
47
+ * terms_accepted= # "1" sets to the current Time, otherwise sets to nil
48
+ * terms_accepted? # true or false
49
+
50
+ You can use it the form-helper like this:
51
+
52
+ # app/views/users/_form.html.erb
53
+ <%= form_for @user do |f| %>
54
+ <p><%= f.date_checkbox :terms_accepted_at %></p>
55
+ <% end -%>
56
+
57
+ The date_checkbox appends the date form the database if its selected. If you do
58
+ not want that, simply use
59
+
60
+ f.checkbox :terms_accepted
61
+
62
+ This is what f.date_checkbox uses anyway.
63
+
64
+
65
+ == Notes
66
+
67
+ Currently, I hook into ActionPack/ActionView directly, which is dirty by saves
68
+ you from declaring a different FormBuilder for every form. I also refrained
69
+ from changing the default-form builder for you because I don't want to conflict
70
+ with other form-extensions you might want to use.
71
+
72
+ If everything goes south, you can still use the model-methods. This is not (by
73
+ any means) über-rocket-science. It's just convenient.
74
+
75
+ Your turn.
76
+
77
+
78
+ == Development
79
+
80
+ Cpt. Obvious told me the following:
81
+
82
+ This is open-sourced on github.
83
+ You can contribute by sending pull-requests or just opening issues.
84
+
85
+ He is obviously right and you also knew that. Any feedback is appreciated.
86
+
87
+
88
+ Copyright (c) 2011 Matthias Viehweger, released under the MIT license
@@ -0,0 +1,31 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+ require 'sdoc'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the date_checkbox plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the date_checkbox plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ require File.expand_path('../lib/date_checkbox/version', __FILE__)
20
+
21
+ rdoc.rdoc_dir = 'doc/rdoc'
22
+ rdoc.title = "DateCheckbox #{DateCheckbox::VERSION}"
23
+ rdoc.options << '--line-numbers' << '--inline-source'
24
+ rdoc.options << '--charset=utf-8'
25
+ rdoc.options << '--fmt=shtml'
26
+
27
+ DateCheckbox::EXTRA_RDOC_FILES.each do |file|
28
+ rdoc.rdoc_files.include(file)
29
+ end
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ - make test run indepently
2
+ => this might mean that a host app needs to be generated.
3
+ after all, I depend on AR::Base and do only a little meta-progamming...
4
+ - put onto travis
5
+ - implement has_date_checkboxes *args
@@ -0,0 +1,30 @@
1
+ # vim:ft=ruby:fileencoding=utf-8
2
+
3
+ require File.expand_path('../lib/date_checkbox/version.rb', __FILE__)
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = "date_checkbox"
7
+ s.version = DateCheckbox::VERSION
8
+ s.date = DateCheckbox::DATE
9
+ s.summary = DateCheckbox::SUMMARY
10
+ s.description = DateCheckbox::DESCRIPTION
11
+
12
+ s.authors = ["Matthias Viehweger"]
13
+ s.email = 'kronn@kronn.de'
14
+ s.homepage = 'http://github.com/kronn/date_checkbox'
15
+ s.rubyforge_project = '[none]' # to supress the warning
16
+
17
+ s.require_paths = ["lib"]
18
+ s.files = `git ls-files`.split("\n") - ['.gitignore']
19
+ s.test_files = `git ls-files test`.split("\n")
20
+
21
+ s.rdoc_options = ['--charset=utf-8', '--fmt=shtml', '--all']
22
+ s.extra_rdoc_files = DateCheckbox::EXTRA_RDOC_FILES
23
+
24
+ s.add_dependency 'rails', '~> 3.0.0'
25
+
26
+ # for release and doc generation, more less optional
27
+ s.add_development_dependency 'rake'
28
+ s.add_development_dependency 'rdoc', '>= 2.4.2'
29
+ s.add_development_dependency 'sdoc'
30
+ end
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,7 @@
1
+ module DateCheckbox
2
+ autoload :HasDateCheckbox, 'date_checkbox/has_date_checkbox'
3
+ autoload :FormHelper, 'date_checkbox/form_helper'
4
+ end
5
+
6
+ ActiveRecord::Base.send(:include, DateCheckbox::HasDateCheckbox)
7
+ ActionView::Helpers::FormBuilder.send(:include, DateCheckbox::FormHelper)
@@ -0,0 +1,12 @@
1
+ module DateCheckbox
2
+ module FormHelper
3
+ def date_checkbox(attribute, options = {})
4
+ long, short = ( attribute.to_s =~ /_at$/ ) ?
5
+ [ attribute, attribute.to_s.sub(/_at$/, '') ] :
6
+ [ :"#{attribute}_at", attribute ]
7
+
8
+ info = object.send(long).to_date if object.send(:"#{short}?")
9
+ check_box(short) + info.to_s
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ module DateCheckbox
2
+ module HasDateCheckbox
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def has_date_checkbox(db_field)
9
+ # if db_field.to_s =~ /_at$/ #&& has_attribute?(db_field)
10
+ column_name = db_field
11
+ method_name = db_field.to_s.sub(/_at$/, '')
12
+ # elsif has_attribute?(:"#{db_field}_at")
13
+ # else
14
+ # column_name = :"#{db_field}_at"
15
+ # method_name = db_field.to_s
16
+ # end
17
+
18
+ # send :include, InstanceMethods
19
+ class_eval do
20
+ define_method("#{method_name}") do
21
+ if attribute_present?(column_name)
22
+ "1" # I18n.t(:"is_#{method_name}")
23
+ else
24
+ "0" # I18n.t(:"not_#{method_name}")
25
+ end
26
+ end
27
+
28
+ define_method("#{method_name}?") do
29
+ attribute_present?(column_name)
30
+ end
31
+
32
+ define_method("#{method_name}=") do |value|
33
+ if value.to_s == "0"
34
+ write_attribute(column_name, nil)
35
+ else
36
+ write_attribute(column_name, Time.now)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # module InstanceMethods
44
+ # end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # vim:ft=ruby:fileencoding=utf-8
2
+
3
+ module DateCheckbox
4
+ VERSION = '0.0.1'
5
+ DATE = File.mtime(__FILE__)
6
+
7
+ SUMMARY = 'Rails-plugin to create a checkbox for datetime-attributes.'
8
+ DESCRIPTION = <<-EOD
9
+ This is a rails-plugin which can create a checkbox for attributes which are
10
+ datetime-fields in the database. So, if you want to store the date when a user
11
+ appected some terms or received some goods, but want to circumvent the need of
12
+ selecting the proper date and time from 6 dropdowns, you can just use this
13
+ plugin.
14
+
15
+ It creates some wrapper-methods on the model for the attributes you specify and
16
+ provides a form-helper which uses these. The model-method also work with
17
+ standard checkbox-tags
18
+ EOD
19
+
20
+ EXTRA_RDOC_FILES = ['README.rdoc']
21
+ end
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'date_checkbox'
@@ -0,0 +1,3 @@
1
+ Dir[File.expand_path('../*_test.rb', __FILE__)].each do |file|
2
+ require file
3
+ end
@@ -0,0 +1,22 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: db/date_checkbox_plugin.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :dbfile: db/date_checkbox_plugin.sqlite3.db
8
+
9
+ postgresql:
10
+ :adapter: postgresql
11
+ :username: postgres
12
+ :password: postgres
13
+ :database: date_checkbox_plugin_test
14
+ :min_messages: ERROR
15
+
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password: password
21
+ :database: date_checkbox_plugin_test
22
+
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class DateCheckboxTest < ActiveSupport::TestCase
4
+ # implementation and infratructure details
5
+ test "schema is loaded" do
6
+ assert_equal [], PublishedAtPost.all
7
+ end
8
+
9
+ # test "a module with the formtag exists" do
10
+ # flunk
11
+ # end
12
+
13
+ # test "the formtag-module is mixed into tha default formbuilder" do
14
+ # flunk
15
+ # end
16
+
17
+ # test "the formtag-method is available" do
18
+ # flunk
19
+ # end
20
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class DateCheckboxFormHelperTest < ActiveSupport::TestCase
4
+ test "form-helpers include the the date_checkbox-helper" do
5
+ assert_respond_to instance, :date_checkbox
6
+ assert_equal -2, instance.method(:date_checkbox).arity
7
+ end
8
+
9
+ test "helper outputs a checkbox" do
10
+ assert_nothing_raised(Exception) do
11
+ result = instance.date_checkbox :published_at
12
+ assert_equal "check_box published", result
13
+
14
+ result = instance.date_checkbox :published
15
+ assert_equal "check_box published", result
16
+ end
17
+ end
18
+
19
+ test "helper output the info after the checkbox" do
20
+ assert_nothing_raised(Exception) do
21
+ instance = instance(:published_at => Time.now)
22
+ result = instance.date_checkbox :published_at
23
+ assert_match(/^check_box published\d{4}-\d{2}-\d{2}$/, result)
24
+
25
+ result = instance.date_checkbox :published
26
+ assert_match(/^check_box published\d{4}-\d{2}-\d{2}$/, result)
27
+ end
28
+ end
29
+
30
+ def klass
31
+ @klass ||= Class.new() do
32
+ include DateCheckbox::FormHelper
33
+
34
+ attr_reader :object
35
+
36
+ def initialize(object_args = {})
37
+ @object = PublishedAtPost.new(object_args)
38
+ end
39
+
40
+ def check_box(sym)
41
+ "check_box #{sym}"
42
+ end
43
+ end
44
+ end
45
+
46
+ def instance(object_args = {})
47
+ @instance ||= klass.new(object_args)
48
+ end
49
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class HasDateCheckboxTest < ActiveSupport::TestCase
4
+ def teardown
5
+ @klass = nil
6
+ @instance = nil
7
+ end
8
+
9
+ test "a class method for one field is added" do
10
+ assert_respond_to klass, :has_date_checkbox
11
+ assert_equal 1, klass.method(:has_date_checkbox).arity
12
+ end
13
+
14
+ test "has_date_checkbox defines a custom reader for the given attribute" do
15
+ assert_respond_to instance, :published
16
+ assert_respond_to instance, :published?
17
+ end
18
+
19
+ test "returns something falsish when attribute is not set" do
20
+ instance.published_at = nil
21
+ assert_equal false, instance.published?
22
+ assert_equal '0', instance.published
23
+ # assert_equal I18n.t(:not_published), instance.published
24
+ end
25
+
26
+ test "returns something trueish when attribute is set" do
27
+ instance.published_at = 1.minute.ago
28
+ assert_equal true, instance.published?
29
+ assert_equal '1', instance.published
30
+ # assert_equal I18n.t(:is_published), instance.published
31
+ end
32
+
33
+ test "has_date_checkbox defines a custom writer for the given attribute" do
34
+ assert_respond_to instance, :'published='
35
+ end
36
+
37
+ test "can be reset to 'not yet'" do
38
+ instance.published = "0"
39
+ assert !instance.published?
40
+ end
41
+
42
+ test "can be set to 'right now'" do
43
+ instance.published = "1"
44
+ assert instance.published?
45
+ end
46
+
47
+ test "works with update_attributes" do
48
+ assert !instance.published?
49
+ instance.update_attributes!({:published => "1"})
50
+ assert instance.published?
51
+ instance.update_attributes!({:published => "0"})
52
+ assert !instance.published?
53
+ end
54
+
55
+ protected
56
+
57
+ def klass
58
+ @klass ||= Class.new do
59
+ include DateCheckbox::HasDateCheckbox
60
+ end
61
+ end
62
+
63
+ def instance
64
+ @instance ||= PublishedAtPost.new
65
+ end
66
+ end
@@ -0,0 +1,10 @@
1
+ # vendor/plugins/date_checkbox/test/schema.rb
2
+
3
+ ActiveRecord::Schema.define(:version => 0) do
4
+ create_table :published_at_posts, :force => true do |t|
5
+ t.string :name
6
+ t.datetime :published_at
7
+ t.timestamps
8
+ end
9
+ end
10
+
@@ -0,0 +1,40 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require 'test/unit'
5
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
6
+
7
+ def load_schema
8
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
9
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
10
+
11
+ db_adapter = ENV['DB']
12
+
13
+ # no db passed, try one of these fine config-free DBs before bombing.
14
+ db_adapter ||=
15
+ begin
16
+ require 'rubygems'
17
+ require 'sqlite'
18
+ 'sqlite'
19
+ rescue MissingSourceFile
20
+ begin
21
+ require 'sqlite3'
22
+ 'sqlite3'
23
+ rescue MissingSourceFile
24
+ end
25
+ end
26
+
27
+ if db_adapter.nil?
28
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
29
+ end
30
+
31
+ ActiveRecord::Base.establish_connection(config[db_adapter])
32
+ load(File.dirname(__FILE__) + "/schema.rb")
33
+ require File.dirname(__FILE__) + '/../init'
34
+ end
35
+
36
+ load_schema
37
+
38
+ class PublishedAtPost < ActiveRecord::Base
39
+ has_date_checkbox :published_at
40
+ end
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_checkbox
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matthias Viehweger
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-14 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rdoc
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 27
60
+ segments:
61
+ - 2
62
+ - 4
63
+ - 2
64
+ version: 2.4.2
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: sdoc
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: " This is a rails-plugin which can create a checkbox for attributes which are\n datetime-fields in the database. So, if you want to store the date when a user\n appected some terms or received some goods, but want to circumvent the need of\n selecting the proper date and time from 6 dropdowns, you can just use this\n plugin.\n\n It creates some wrapper-methods on the model for the attributes you specify and\n provides a form-helper which uses these. The model-method also work with\n standard checkbox-tags\n"
82
+ email: kronn@kronn.de
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - README.rdoc
89
+ files:
90
+ - Gemfile
91
+ - MIT-LICENSE
92
+ - README.rdoc
93
+ - Rakefile
94
+ - TODO
95
+ - date_checkbox.gemspec
96
+ - install.rb
97
+ - lib/date_checkbox.rb
98
+ - lib/date_checkbox/form_helper.rb
99
+ - lib/date_checkbox/has_date_checkbox.rb
100
+ - lib/date_checkbox/version.rb
101
+ - rails/init.rb
102
+ - test/all.rb
103
+ - test/database.yml
104
+ - test/date_checkbox_test.rb
105
+ - test/form_helper_test.rb
106
+ - test/has_date_checkbox_test.rb
107
+ - test/schema.rb
108
+ - test/test_helper.rb
109
+ - uninstall.rb
110
+ has_rdoc: true
111
+ homepage: http://github.com/kronn/date_checkbox
112
+ licenses: []
113
+
114
+ post_install_message:
115
+ rdoc_options:
116
+ - --charset=utf-8
117
+ - --fmt=shtml
118
+ - --all
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: 3
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ requirements: []
140
+
141
+ rubyforge_project: "[none]"
142
+ rubygems_version: 1.6.2
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Rails-plugin to create a checkbox for datetime-attributes.
146
+ test_files:
147
+ - test/all.rb
148
+ - test/database.yml
149
+ - test/date_checkbox_test.rb
150
+ - test/form_helper_test.rb
151
+ - test/has_date_checkbox_test.rb
152
+ - test/schema.rb
153
+ - test/test_helper.rb