date_flag 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9106544c22673b2a978bf4704309a8ce953b53ba
4
+ data.tar.gz: 90341cd241cf7ce6d3c5ced970a1595ce549732c
5
+ SHA512:
6
+ metadata.gz: 784580c900ddcb3db6e0eb848da662cb1a217f2c41af4b03291c5f3931415af91078a43560e738eb3036c9ac40625a9c888c6087cadfabc53ed7995bd2a51f29
7
+ data.tar.gz: 59aa706183c4eed6c606ce61d41b9bb02c5443dec6c71ddf8ddec4554843a7f7c2f268f9138884764fbb2b5313f3a19da588ccc60edb9f5fc86e63dbfba42ea6
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gem 'activerecord', '>= 3.0.0'
4
+
5
+ group :development do
6
+ gem 'jeweler'
7
+ gem 'rdoc'
8
+ gem 'rails'
9
+ gem 'sqlite3'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,145 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (4.2.6)
5
+ actionpack (= 4.2.6)
6
+ actionview (= 4.2.6)
7
+ activejob (= 4.2.6)
8
+ mail (~> 2.5, >= 2.5.4)
9
+ rails-dom-testing (~> 1.0, >= 1.0.5)
10
+ actionpack (4.2.6)
11
+ actionview (= 4.2.6)
12
+ activesupport (= 4.2.6)
13
+ rack (~> 1.6)
14
+ rack-test (~> 0.6.2)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
17
+ actionview (4.2.6)
18
+ activesupport (= 4.2.6)
19
+ builder (~> 3.1)
20
+ erubis (~> 2.7.0)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
+ activejob (4.2.6)
24
+ activesupport (= 4.2.6)
25
+ globalid (>= 0.3.0)
26
+ activemodel (4.2.6)
27
+ activesupport (= 4.2.6)
28
+ builder (~> 3.1)
29
+ activerecord (4.2.6)
30
+ activemodel (= 4.2.6)
31
+ activesupport (= 4.2.6)
32
+ arel (~> 6.0)
33
+ activesupport (4.2.6)
34
+ i18n (~> 0.7)
35
+ json (~> 1.7, >= 1.7.7)
36
+ minitest (~> 5.1)
37
+ thread_safe (~> 0.3, >= 0.3.4)
38
+ tzinfo (~> 1.1)
39
+ addressable (2.4.0)
40
+ arel (6.0.3)
41
+ builder (3.2.2)
42
+ concurrent-ruby (1.0.1)
43
+ descendants_tracker (0.0.4)
44
+ thread_safe (~> 0.3, >= 0.3.1)
45
+ erubis (2.7.0)
46
+ faraday (0.9.2)
47
+ multipart-post (>= 1.2, < 3)
48
+ git (1.3.0)
49
+ github_api (0.13.1)
50
+ addressable (~> 2.4.0)
51
+ descendants_tracker (~> 0.0.4)
52
+ faraday (~> 0.8, < 0.10)
53
+ hashie (>= 3.4)
54
+ multi_json (>= 1.7.5, < 2.0)
55
+ oauth2
56
+ globalid (0.3.6)
57
+ activesupport (>= 4.1.0)
58
+ hashie (3.4.3)
59
+ highline (1.7.8)
60
+ i18n (0.7.0)
61
+ jeweler (2.0.1)
62
+ builder
63
+ bundler (>= 1.0)
64
+ git (>= 1.2.5)
65
+ github_api
66
+ highline (>= 1.6.15)
67
+ nokogiri (>= 1.5.10)
68
+ rake
69
+ rdoc
70
+ json (1.8.3)
71
+ jwt (1.5.1)
72
+ loofah (2.0.3)
73
+ nokogiri (>= 1.5.9)
74
+ mail (2.6.4)
75
+ mime-types (>= 1.16, < 4)
76
+ mime-types (3.0)
77
+ mime-types-data (~> 3.2015)
78
+ mime-types-data (3.2016.0221)
79
+ mini_portile2 (2.0.0)
80
+ minitest (5.8.4)
81
+ multi_json (1.11.2)
82
+ multi_xml (0.5.5)
83
+ multipart-post (2.0.0)
84
+ nokogiri (1.6.7.2)
85
+ mini_portile2 (~> 2.0.0.rc2)
86
+ oauth2 (1.1.0)
87
+ faraday (>= 0.8, < 0.10)
88
+ jwt (~> 1.0, < 1.5.2)
89
+ multi_json (~> 1.3)
90
+ multi_xml (~> 0.5)
91
+ rack (>= 1.2, < 3)
92
+ rack (1.6.4)
93
+ rack-test (0.6.3)
94
+ rack (>= 1.0)
95
+ rails (4.2.6)
96
+ actionmailer (= 4.2.6)
97
+ actionpack (= 4.2.6)
98
+ actionview (= 4.2.6)
99
+ activejob (= 4.2.6)
100
+ activemodel (= 4.2.6)
101
+ activerecord (= 4.2.6)
102
+ activesupport (= 4.2.6)
103
+ bundler (>= 1.3.0, < 2.0)
104
+ railties (= 4.2.6)
105
+ sprockets-rails
106
+ rails-deprecated_sanitizer (1.0.3)
107
+ activesupport (>= 4.2.0.alpha)
108
+ rails-dom-testing (1.0.7)
109
+ activesupport (>= 4.2.0.beta, < 5.0)
110
+ nokogiri (~> 1.6.0)
111
+ rails-deprecated_sanitizer (>= 1.0.1)
112
+ rails-html-sanitizer (1.0.3)
113
+ loofah (~> 2.0)
114
+ railties (4.2.6)
115
+ actionpack (= 4.2.6)
116
+ activesupport (= 4.2.6)
117
+ rake (>= 0.8.7)
118
+ thor (>= 0.18.1, < 2.0)
119
+ rake (11.1.2)
120
+ rdoc (4.2.2)
121
+ json (~> 1.4)
122
+ sprockets (3.5.2)
123
+ concurrent-ruby (~> 1.0)
124
+ rack (> 1, < 3)
125
+ sprockets-rails (3.0.4)
126
+ actionpack (>= 4.0)
127
+ activesupport (>= 4.0)
128
+ sprockets (>= 3.0.0)
129
+ sqlite3 (1.3.11)
130
+ thor (0.19.1)
131
+ thread_safe (0.3.5)
132
+ tzinfo (1.2.2)
133
+ thread_safe (~> 0.1)
134
+
135
+ PLATFORMS
136
+ ruby
137
+
138
+ DEPENDENCIES
139
+ jeweler
140
+ rails
141
+ rdoc
142
+ sqlite3
143
+
144
+ BUNDLED WITH
145
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2014 The Working Group Inc.
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 NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Simple Date Flag Support for Rails
2
+
3
+ This is a date flag method for ActiveRecord models and works with Rails 3.x
4
+ or newer.
5
+
6
+ A date flag is a DATETIME type column that behaves as a boolean. This helps
7
+ when tracking things such as if article is published or if a user is banned
8
+ while preserving information about when that event did or should occur.
9
+
10
+ Dates can be in the past or in the future and the meaning of the flag is
11
+ subjective.
12
+
13
+ ## Installation
14
+
15
+ This can be installed as a Ruby gem by adding the following to your `Gemfile`:
16
+
17
+ gem 'date_flag'
18
+
19
+ ## Usage
20
+
21
+ Inside your model if you have a column `banned_at`:
22
+
23
+ date_flag :banned_at
24
+
25
+ This will introduce methods `banned?` for quick testing and `banned!` for
26
+ immediately setting this flag and saving the record. There's also a `banned`
27
+ scope established for that model.
28
+
29
+ There are several options that can customize the name of the methods:
30
+
31
+ * `:name` defines the base name for this flag and defaults to the name of the
32
+ column minus the trailing `_at`.
33
+ * `:scope` can be set to a a symbol or string, in which case that's the name of
34
+ the generated scope, to `false` to skip creating the scope.
35
+ * `:inverse` can be set with a symbol or string which will create a scope of
36
+ this name that behaves in the opposite manner to the main scope.
37
+ * `:action` can be set with a symbol or string that defines the name used
38
+ when generating accessor methods.
39
+ * `:inverse_action` can be set with a symbol or string which will create a
40
+ method to unset the flag with the supplied name. If `:inverse` is set, that
41
+ name will be used by default.
42
+
43
+ These might be used together like this:
44
+
45
+ date_flag :was_published_at,
46
+ name: :published,
47
+ scope: :published,
48
+ inverse: :unpublished,
49
+ inverse_action: :unpublish!,
50
+ action: :publish
51
+
52
+ This results in instance methods `publish!` but the scopes are named
53
+ `published` and `unpublished`. The flag itself is also available via the
54
+ `published?` and `published=` methods.
55
+
56
+ ## License
57
+
58
+ (C) 2009-2016 Scott Tadman, [The Working Group Inc.](http://twg.ca/)
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task default: :test
8
+
9
+ desc 'Test the date_flag 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
+ require 'jeweler'
18
+
19
+ Jeweler::Tasks.new do |gem|
20
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
21
+ gem.name = "date_flag"
22
+ gem.summary = %Q{Date field exension for ActiveRecord}
23
+ gem.description = %Q{Represents boolean fields as DateTime values to trigger events in the future or record when events happened in the past.}
24
+ gem.email = 'scott@twg.ca'
25
+ gem.homepage = "http://github.com/twg/date_flag"
26
+ gem.authors = [
27
+ "Scott Tadman <scott@twg.ca>"
28
+ ]
29
+ end
30
+
31
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'date_flag'
2
+
3
+ ActiveRecord::Base.send(:extend, DateFlag)
data/lib/date_flag.rb ADDED
@@ -0,0 +1,128 @@
1
+ module DateFlag
2
+ VERSION = File.read(File.expand_path('../VERSION', File.dirname(__FILE__))).chomp
3
+
4
+ # Usage:
5
+ #
6
+ # class MyModel < ActiveRecord::Base
7
+ # date_flag :flagged_at, action: :flag
8
+ # end
9
+ #
10
+ # m = MyModel.new
11
+ # m.flagged? # => false
12
+ # m.flag! # Assigns flag_at to current time
13
+ # m.flag = true # Same as flag!
14
+ # m.flagged? # => true
15
+
16
+ def date_flag(field, options = { })
17
+ name = (options[:name] ? options[:name] : field.to_s.sub(/_at$/, '')).to_sym
18
+ action = (options[:action] ? options[:action] : name).to_sym
19
+
20
+ scope_name =
21
+ case (options[:scope])
22
+ when nil, true
23
+ name
24
+ when false
25
+ false
26
+ else
27
+ options[:scope].to_s.to_sym
28
+ end
29
+
30
+ case (scope_name)
31
+ when false, nil
32
+ # Skip this operation
33
+ when :send, :id
34
+ # TODO: Invalid names, should raise exception or warning
35
+ else
36
+ scope scope_name, lambda { |*flag|
37
+ case (flag.first)
38
+ when false
39
+ where(field => nul)
40
+ when true, nil
41
+ where.not(field => nil)
42
+ else
43
+ # FIX: Escape properly for Postgres/MySQL
44
+ where([ "#{field}<=?", flag.first ])
45
+ end
46
+ }
47
+ end
48
+
49
+ if (options[:inverse])
50
+ scope options[:inverse], lambda { |*flag|
51
+ case (flag.first)
52
+ when false
53
+ where.not(field => nil)
54
+ when true, nil
55
+ where(field => nil)
56
+ else
57
+ # FIX: Escape properly for Postgres/MySQL
58
+ where([ "#{field}>?", flag.first ])
59
+ end
60
+ }
61
+ end
62
+
63
+ if (inverse_action = options[:inverse_action] || options[:inverse])
64
+ define_method(:"#{inverse_action}!") do
65
+ write_attribute(field, nil)
66
+
67
+ save!
68
+ end
69
+ end
70
+
71
+ define_method(:"#{name}=") do |value|
72
+ # The action= mutator method is used to manipulate the trigger time.
73
+ # Values of nil, false, empty string, '0' or 0 are presumed to be
74
+ # false and will nil out the time. A DateTime, Date or Time object
75
+ # will be saved as-is, and anything else will just assign the current
76
+ # time.
77
+
78
+ case (value)
79
+ when nil, false, '', '0', 0
80
+ write_attribute(field, nil)
81
+ when DateTime, Date, Time
82
+ write_attribute(field, value)
83
+ else
84
+ !read_attribute(field) and write_attribute(field, Time.now)
85
+ end
86
+ end
87
+
88
+ define_method(:"#{name}") do
89
+ value = read_attribute(field)
90
+
91
+ value ? (value <= Time.now) : false
92
+ end
93
+
94
+ define_method(:"#{name}?") do
95
+ # The name? accessor method will return true if the date is defined
96
+ # and is prior to the current time, or false otherwise.
97
+ value = read_attribute(field)
98
+
99
+ value ? (value <= Time.now) : false
100
+ end
101
+
102
+ define_method(:"#{action}!") do |*at_time|
103
+ # The name! method is used to set the trigger time. If the time is
104
+ # already defined and is in the past, then the time is left unchanged.
105
+ # If it is undefined or in the future, then the current time is
106
+ # substituted.
107
+
108
+ value = read_attribute(field)
109
+
110
+ at_time =
111
+ case (at_time.first)
112
+ when false
113
+ nil
114
+ else
115
+ at_time.first || value || Time.now
116
+ end
117
+
118
+ return if (at_time == value)
119
+
120
+ write_attribute(field, at_time)
121
+ save!
122
+ end
123
+ end
124
+ end
125
+
126
+ if (defined?(ActiveRecord))
127
+ ActiveRecord::Base.extend(DateFlag)
128
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class DateFlagTest < ActiveSupport::TestCase
4
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_flag
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Tadman <scott@twg.ca>
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Represents boolean fields as DateTime values to trigger events in the
84
+ future or record when events happened in the past.
85
+ email: scott@twg.ca
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - LICENSE
90
+ - README.md
91
+ files:
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - VERSION
98
+ - init.rb
99
+ - lib/date_flag.rb
100
+ - test/date_flag_test.rb
101
+ - test/test_helper.rb
102
+ homepage: http://github.com/twg/date_flag
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.5.1
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Date field exension for ActiveRecord
125
+ test_files: []