acts_as_boolean 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 773ce9e1980e78ca77409a5a4f9623659f22c05a2b53f4c2fa99eca07b5ee625
4
+ data.tar.gz: ee87c762220fd1ce25916121eddedc95745ab6678ff06093d4cc5f047d92c60f
5
+ SHA512:
6
+ metadata.gz: 22fe0d3e4f8ff53c831a8cce6dbe60c2eda8fcb4642b4fe10081481b5644ccd6b7b9b3a4ec17b6cf8c0cbc92aeb392da29f6f6f2315702cd6b053397daccbadb
7
+ data.tar.gz: b2de00dc3df44d5ef8be00538aa237363973fcbd84f7f5d3d6c154f7b1635e9d74a1e5901e55f35a69198caa2ad0ab381580ebce9b22933b3607064d05d6458a
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Josh Brody
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,110 @@
1
+ # ActsAsBoolean
2
+
3
+ Treat time-y attributes like boolean attributes.
4
+
5
+ ## Why
6
+
7
+ Ruby is expressive. Expressive is sexy. ActiveRecord time-y objects are time-y objects. Time-y objects are a nightmare, and therefore, not sexy.
8
+
9
+ We like sexy.
10
+
11
+ ## How
12
+
13
+ class Post < ApplicationRecord
14
+ acts_as_boolean :published_at
15
+ end
16
+
17
+ class PostsController < ApplicationController
18
+ def index
19
+ @posts = Post.published
20
+ end
21
+
22
+ def show
23
+ @post = Post.find(params[:id])
24
+ unless @post.published?
25
+ raise ActiveRecord::RecordNotFound
26
+ end
27
+ end
28
+ end
29
+
30
+ ## What
31
+
32
+ Given `Post#published_at`:
33
+
34
+ | Method | Description
35
+ | ----------------- | -----------
36
+ | `Post#published` | `true` if `Post#published_at` is greater than or equal to `Time.current`; otherwise `false`
37
+ | `Post#published?` | Sexy alias of `Post#published`
38
+ | `Post#published=` | Sets the value of `published_at` to `Time.current` if the value passed is truth-y per `ActiveModel::Type::Boolean`; otherwise sets `published_at` to nil
39
+ | `Post.published` | Creates a scope of `Post.where('published_at <= ?', Time.current).where.not(published_at: nil)`
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ ```ruby
46
+ gem 'acts_as_boolean'
47
+ ```
48
+
49
+ And then execute:
50
+
51
+ ```bash
52
+ $ bundle
53
+ ```
54
+
55
+ The great migration — you probably want an index here because we create a scope to the column:
56
+
57
+ `rails g migration AddPublishedAtToPosts published_at:datetime:index`
58
+
59
+ `rake db:migrate`
60
+
61
+ Then drop it in:
62
+
63
+ class Post < ApplicationRecord
64
+ acts_as_boolean :published_at
65
+ end
66
+
67
+ ## Advanced configuration
68
+
69
+ ### Default time-y thing
70
+
71
+ By default, `ActsAsBoolean` uses `Time.current`. Want to use `DateTime.now`? Great, just send in a `proc`.
72
+
73
+ In `config/initializers/acts_as_boolean.rb`:
74
+
75
+ ```ruby
76
+ ActsAsBoolean.timeish = -> { DateTime.now }
77
+ ```
78
+
79
+ Want more control? Pass it in.
80
+
81
+ ```ruby
82
+ class Post < ApplicationRecord
83
+ acts_as_boolean :published_at, time: -> { DateTime.now }
84
+ end
85
+ ```
86
+ ### New method name
87
+
88
+ By default, `ActsAsBoolean` assumes you want `column_name.to_s.delete_suffix("_at")`. Given you have `published_pie` as your column name (which, of course, you do, because you love pie)...
89
+
90
+ ```ruby
91
+ class Post < ApplicationRecord
92
+ acts_as_boolean :published_pie, as: :published
93
+ end
94
+ ```
95
+
96
+ Want to control this globally?
97
+
98
+ In `config/initializers/acts_as_boolean.rb`:
99
+
100
+ ```ruby
101
+ ActsAsBoolean.normalize_column = -> (str) { str.gsub("_at", "") }
102
+ ```
103
+
104
+ ## Contributing
105
+
106
+ Yes.
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsAsBoolean'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,38 @@
1
+ require "acts_as_boolean/railtie"
2
+
3
+ module ActsAsBoolean
4
+ mattr_accessor :timeish, default: -> { Time.current }
5
+ mattr_accessor :normalize_column, default: -> (str) { str.to_s.delete_suffix("_at") }
6
+
7
+ module Extension
8
+ def acts_as_boolean(column_name, options = {})
9
+ options[:as] ||= ActsAsBoolean.normalize_column.call(column_name)
10
+ options[:time] ||= ActsAsBoolean.timeish
11
+ define_booleany_reader(column_name, options[:as], options[:time]) unless options[:reader]
12
+ define_booleany_writer(column_name, options[:as], options[:time]) unless options[:writer]
13
+ define_booleany_scope(column_name, options[:as], options[:time]) unless options[:scope]
14
+ end
15
+
16
+ def define_booleany_reader(original_column, booleany_column, time)
17
+ define_method("#{booleany_column}") do
18
+ return false if send(original_column).nil?
19
+ send(original_column) <= time.call
20
+ end
21
+ alias_method "#{booleany_column}?", booleany_column
22
+ end
23
+
24
+ def define_booleany_writer(original_column, booleany_column, time)
25
+ define_method("#{booleany_column}=") do |value|
26
+ if ActiveModel::Type::Boolean::FALSE_VALUES.exclude?(value)
27
+ send("#{original_column}=", time.call)
28
+ else
29
+ send("#{original_column}=", nil)
30
+ end
31
+ end
32
+ end
33
+
34
+ def define_booleany_scope(original_column, booleany_column, time)
35
+ scope booleany_column.to_sym, -> { where("#{original_column} <= ?", time.call).where.not(original_column.to_sym => nil) }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module ActsAsBoolean
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'acts_as_boolean' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ActsAsBoolean::Extension
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsBoolean
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_boolean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Treat time-y columns like booleans.
56
+ email:
57
+ - josh@josh.mn
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/acts_as_boolean.rb
66
+ - lib/acts_as_boolean/railtie.rb
67
+ - lib/acts_as_boolean/version.rb
68
+ homepage: https://github.com/joshmn/acts_as_boolean
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Treat time-y columns like booleans.
91
+ test_files: []