unique_by_date 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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unique_by_date.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bryan Ricker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # UniqueByDate
2
+
3
+ A validator for Rails which allows you to validate the uniqueness of an attribute, scoped by a date attribute on the same model. Useful for when your URL's contain a URL slug and a date.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'unique_by_date'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install unique_by_date
19
+
20
+
21
+ ## Usage
22
+
23
+ It works just like a normal validator. It has two special parameters (aside from the ones provided by ActiveModel::EachValidator). Both parameters are required:
24
+
25
+ * `scope` - The date or datetime attribute by which to scope the uniqueness of the attribute being validated. So if you want a `url_slug` attribute to be unique based on a `published_at` timestamp, then you would add `scope: :published_at`.
26
+ * `filter` - How the scoped attribute (i.e. `published_at`) should be filtered when validating. If you want every slug to be unique by each month, you would add `filter: :month`. The validator uses [ActiveSupport's Date Calculations](http://apidock.com/rails/ActiveSupport/CoreExtensions/Date/Calculations), so you may use any of those available filters: `:hour, :day, :week, :month, :quarter, :year`.
27
+
28
+ You should also add a custom message, or add the message to your locales file.
29
+ Here's an example:
30
+
31
+ ```ruby
32
+ class Event < ActiveRecord::Base
33
+ # Schema:
34
+ # create_table :events do |t|
35
+ # t.string :title
36
+ # t.string :url_slug
37
+ # t.datetime :starts_at
38
+ # t.datetime :ends_at
39
+ # end
40
+ #
41
+ # Route:
42
+ # get 'events/:year/:month/:day/:slug' => 'events#show'
43
+
44
+ validates :slug, unique_by_date: {
45
+ :scope => :starts_at,
46
+ :filter => :day,
47
+ :message => "must be unique for each starts_at day."
48
+ }
49
+ end
50
+ ```
51
+
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ require "unique_by_date/version"
2
+ require 'active_record'
3
+ require 'unique_by_date_validator'
4
+
5
+ module UniqueByDate
6
+ end
7
+
@@ -0,0 +1,3 @@
1
+ module UniqueByDate
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,66 @@
1
+ ##
2
+ # Unique By validator
3
+ # Makes sure an attribute is unique by a specified day, month, year, etc.
4
+ # This validator is heavily based on ActiveRecord::Validations::UniquenessValidator
5
+ #
6
+ # It uses the `beginning_of_*` and `end_of_*` methods to
7
+ # specify the range of dates for which the attribute must be unique
8
+ # ActiveSupport::CoreExtensions::Date::Calculations
9
+ # http://apidock.com/rails/ActiveSupport/CoreExtensions/Date/Calculations
10
+ #
11
+ # `scope` must be a datetime field
12
+ # Filter options are: [:hour, :day, :week, :month, :quarter, :year]
13
+ #
14
+ # Usage:
15
+ #
16
+ # validates :slug, unique_by_date: { scope: :starts_at, filter: :day }
17
+ #
18
+ class UniqueByDateValidator < ActiveModel::EachValidator
19
+ def setup(klass)
20
+ @klass = klass
21
+ end
22
+
23
+ #---------------
24
+
25
+ def validate_each(record, attribute, value)
26
+ finder_class = find_finder_class_for(record)
27
+ table = finder_class.arel_table
28
+
29
+ coder = record.class.serialized_attributes[attribute.to_s]
30
+
31
+ if value && coder
32
+ value = coder.dump value
33
+ end
34
+
35
+ relation = table[attribute].eq(value)
36
+ relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
37
+
38
+ scope_value = record.read_attribute(options[:scope])
39
+ return true if !scope_value
40
+
41
+ low_limit = scope_value.send("beginning_of_#{options[:filter]}")
42
+ high_limit = scope_value.send("end_of_#{options[:filter]}")
43
+
44
+ relation = relation.and(table[options[:scope]].gteq(low_limit))
45
+ relation = relation.and(table[options[:scope]].lt(high_limit))
46
+ relation = finder_class.unscoped.where(relation)
47
+
48
+ if relation.exists?
49
+ record.errors.add(attribute, :taken, options.merge(value: value))
50
+ end
51
+ end
52
+
53
+ #---------------
54
+
55
+ protected
56
+
57
+ def find_finder_class_for(record) #:nodoc:
58
+ class_hierarchy = [record.class]
59
+
60
+ while class_hierarchy.first != @klass
61
+ class_hierarchy.prepend(class_hierarchy.first.superclass)
62
+ end
63
+
64
+ class_hierarchy.detect { |klass| !klass.abstract_class? }
65
+ end
66
+ end
@@ -0,0 +1,14 @@
1
+ require 'unique_by_date'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "sqlite3",
5
+ :database => File.join(File.dirname(__FILE__), "test.sqlite3")
6
+ )
7
+
8
+ load File.join(File.dirname(__FILE__), "support", "models.rb")
9
+
10
+ RSpec.configure do |config|
11
+ config.before do
12
+ load File.join(File.dirname(__FILE__), "support", "schema.rb")
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ class Event < ActiveRecord::Base
2
+ validates :slug, unique_by_date: { scope: :starts_at, filter: :day, allow_blank: true, message: "not unique." }
3
+ validates :title, unique_by_date: { scope: :starts_at, filter: :month, allow_blank: true, message: "not unique." }
4
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :events, force: true do |t|
5
+ t.string :title
6
+ t.string :slug
7
+ t.datetime :starts_at
8
+ end
9
+ end
data/spec/test.sqlite3 ADDED
Binary file
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe UniqueByDateValidator do
4
+ it 'is not valid if another exists with given parameters' do
5
+ event1 = Event.create(slug: "same", starts_at: 1.day.ago)
6
+ event2 = Event.new(slug: "same", starts_at: 1.day.ago)
7
+
8
+ event2.should_not be_valid
9
+ end
10
+
11
+ it 'is valid if dates are not in the same scope' do
12
+ event1 = Event.create(slug: "same", starts_at: 3.days.ago)
13
+ event2 = Event.new(slug: "same", starts_at: 1.day.ago)
14
+
15
+ event2.valid?
16
+ puts event2.errors.full_messages
17
+ event2.should be_valid
18
+ end
19
+
20
+ it "is valid if the attribute is already unique" do
21
+ event1 = Event.create(slug: "same", starts_at: 1.day.ago)
22
+ event2 = Event.new(slug: "notsame", starts_at: 1.day.ago)
23
+
24
+ event2.should be_valid
25
+ end
26
+
27
+ it 'can use a different filter' do
28
+ event1 = Event.create(title: "same", starts_at: 1.day.ago)
29
+ event2 = Event.new(title: "same", starts_at: 2.days.ago)
30
+
31
+ # in models.rb, title uniqueness if filtered by month
32
+ event2.should_not be_valid
33
+
34
+ event2.starts_at = 3.months.ago
35
+ event2.should be_valid
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unique_by_date/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unique_by_date"
8
+ spec.version = UniqueByDate::VERSION
9
+ spec.authors = ["Bryan Ricker"]
10
+ spec.email = ["bricker88@gmail.com"]
11
+ spec.description = %q{A Rails validator for uniqueness by date.}
12
+ spec.summary = %q{A validator for Rails which allows you to validate the uniqueness of an attribute, scoped by a date attribute on the same model. Useful for when your URL's contain a URL slug and a date.}
13
+ spec.homepage = "https://github.com/bricker/unique_by_date"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", ">= 3.0.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency "sqlite3-ruby"
27
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unique_by_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan Ricker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3-ruby
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A Rails validator for uniqueness by date.
95
+ email:
96
+ - bricker88@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/unique_by_date.rb
107
+ - lib/unique_by_date/version.rb
108
+ - lib/unique_by_date_validator.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/models.rb
111
+ - spec/support/schema.rb
112
+ - spec/test.sqlite3
113
+ - spec/unique_by_date_validator_spec.rb
114
+ - unique_by_date.gemspec
115
+ homepage: https://github.com/bricker/unique_by_date
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.25
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: A validator for Rails which allows you to validate the uniqueness of an attribute,
140
+ scoped by a date attribute on the same model. Useful for when your URL's contain
141
+ a URL slug and a date.
142
+ test_files:
143
+ - spec/spec_helper.rb
144
+ - spec/support/models.rb
145
+ - spec/support/schema.rb
146
+ - spec/test.sqlite3
147
+ - spec/unique_by_date_validator_spec.rb