acts_as_digested_on 0.0.2
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 +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +70 -0
- data/Rakefile +1 -0
- data/acts_as_digested_on.gemspec +27 -0
- data/lib/acts_as_digested_on.rb +65 -0
- data/lib/acts_as_digested_on/version.rb +3 -0
- data/spec/acts_as_digested_on_spec.rb +105 -0
- data/spec/spec_helper.rb +23 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2012 milk1000cc
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= ActsAsDigestedOn
|
2
|
+
|
3
|
+
acts_as_digested_on is a gem for Rails 3.
|
4
|
+
|
5
|
+
This sets the digested value before validation and validates uniqueness of the digested value.
|
6
|
+
|
7
|
+
If you use this gem for Rails 2, see rails-2 branch.
|
8
|
+
|
9
|
+
== Install
|
10
|
+
|
11
|
+
sudo gem install acts_as_digested_on
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
# db/migrate/20090706000000_create_articles.rb
|
16
|
+
class CreateArticles < ActiveRecord::Migration
|
17
|
+
def self.up
|
18
|
+
create_table :articles do |t|
|
19
|
+
t.text :url # any type is ok
|
20
|
+
t.string :digest # this is sha-1 hex digest field of url value
|
21
|
+
...
|
22
|
+
end
|
23
|
+
|
24
|
+
add_index :articles, :digest, :unique => true # you can add index on digest field
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.down
|
28
|
+
drop_table :articles
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# app/models/article.rb
|
33
|
+
class Article < ActiveRecord::Base
|
34
|
+
acts_as_digested_on :url # please add this
|
35
|
+
...
|
36
|
+
end
|
37
|
+
|
38
|
+
# this means
|
39
|
+
class Article < ActiveRecord::Base
|
40
|
+
validates_uniqueness_of :digest
|
41
|
+
before_validation :set_digest
|
42
|
+
|
43
|
+
def generate_digest
|
44
|
+
Digest::SHA1.hexdigest "--#{ url.to_s }--"
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def set_digest
|
49
|
+
self.digest = generate_digest
|
50
|
+
end
|
51
|
+
...
|
52
|
+
end
|
53
|
+
|
54
|
+
== Examples
|
55
|
+
|
56
|
+
acts_as_digested_on :url, :unique => false
|
57
|
+
|
58
|
+
acts_as_digested_on :url, :digest_column => :url_digest
|
59
|
+
|
60
|
+
acts_as_digested_on [:url, :salt]
|
61
|
+
|
62
|
+
acts_as_digested_on :url, :scope => :site_id
|
63
|
+
|
64
|
+
Article.new(:url => 'http://example.com').generate_digest
|
65
|
+
|
66
|
+
== Copyright
|
67
|
+
|
68
|
+
Copyright (c) 2009-2012 milk1000cc, released under the MIT license
|
69
|
+
|
70
|
+
milk1000cc <mailto:info@milk1000.cc>
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_digested_on/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_digested_on"
|
7
|
+
s.version = ActsAsDigestedOn::VERSION
|
8
|
+
s.authors = ["milk1000cc"]
|
9
|
+
s.email = ["info@milk1000.cc"]
|
10
|
+
s.homepage = "https://github.com/milk1000cc/acts_as_digested_on"
|
11
|
+
s.summary = %q{Automatically set sha-1 hex digest}
|
12
|
+
s.description = %q{This gem sets the digested value before validation and validates uniqueness of the digested value.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "acts_as_digested_on"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
s.add_dependency "rails"
|
25
|
+
s.add_development_dependency "rspec"
|
26
|
+
s.add_development_dependency "sqlite3"
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "acts_as_digested_on/version"
|
2
|
+
require 'digest/sha1'
|
3
|
+
|
4
|
+
module ActsAsDigestedOn
|
5
|
+
def self.included(model)
|
6
|
+
model.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module Validations
|
10
|
+
def self.included(model)
|
11
|
+
model.class_eval do
|
12
|
+
options = acts_as_digested_on_vars[:validates_uniqueness_of_options]
|
13
|
+
validates_uniqueness_of acts_as_digested_on_vars[:digest_column], options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Callbacks
|
19
|
+
def self.included(model)
|
20
|
+
model.class_eval do
|
21
|
+
before_validation :set_digest
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module InstanceMethods
|
27
|
+
def generate_digest
|
28
|
+
original_columns = self.class.acts_as_digested_on_vars[:original_columns]
|
29
|
+
original_string = "--#{ original_columns.map { |v| self[v].to_s }.join('--') }--"
|
30
|
+
digest = Digest::SHA1.hexdigest(original_string)
|
31
|
+
digest.encode! 'utf-8' if RUBY_VERSION.to_f >= 1.9
|
32
|
+
digest
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def set_digest
|
37
|
+
digest_column = self.class.acts_as_digested_on_vars[:digest_column]
|
38
|
+
self[digest_column] = generate_digest
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods
|
43
|
+
def acts_as_digested_on(original_columns, options = {})
|
44
|
+
options = options.symbolize_keys
|
45
|
+
|
46
|
+
original_columns = Array(original_columns).flatten
|
47
|
+
digest_column = options.delete(:digest_column) || 'digest'
|
48
|
+
unique = options.key?(:unique) ? options.delete(:unique) : true
|
49
|
+
|
50
|
+
class_attribute :acts_as_digested_on_vars
|
51
|
+
self.acts_as_digested_on_vars = {
|
52
|
+
:original_columns => original_columns,
|
53
|
+
:digest_column => digest_column,
|
54
|
+
:unique => unique,
|
55
|
+
:validates_uniqueness_of_options => options
|
56
|
+
}
|
57
|
+
|
58
|
+
include InstanceMethods
|
59
|
+
include Callbacks
|
60
|
+
include Validations if self.acts_as_digested_on_vars[:unique]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
ActiveRecord::Base.send :include, ActsAsDigestedOn
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Article < ActiveRecord::Base
|
4
|
+
define_table do |t|
|
5
|
+
t.integer :user_id
|
6
|
+
t.string :title
|
7
|
+
t.text :url
|
8
|
+
t.string :digest
|
9
|
+
t.string :my_digest
|
10
|
+
t.text :content
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "ActsAsDigestedOn" do
|
15
|
+
before do
|
16
|
+
Article.reset_callbacks :validate
|
17
|
+
Article.destroy_all
|
18
|
+
end
|
19
|
+
|
20
|
+
before do
|
21
|
+
@url = 'http://example.com'
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#generate_digest' do
|
25
|
+
it 'should generate sha-1 digest of target field with some string' do
|
26
|
+
Article.acts_as_digested_on :url
|
27
|
+
|
28
|
+
article = Article.new(:url => @url)
|
29
|
+
article.generate_digest.should == Digest::SHA1.hexdigest("--#{ @url }--")
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'when more than one target field is given' do
|
33
|
+
it 'should generate sha-1 digest of target fields with some string' do
|
34
|
+
Article.acts_as_digested_on [:title, :url]
|
35
|
+
|
36
|
+
title = 'an article'
|
37
|
+
article = Article.new(:title => title, :url => @url)
|
38
|
+
article.generate_digest.should == Digest::SHA1.hexdigest("--#{ title }--#{ @url }--")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'before validation' do
|
44
|
+
it 'should set "digest" field' do
|
45
|
+
Article.acts_as_digested_on :url
|
46
|
+
|
47
|
+
article = Article.new(:url => @url)
|
48
|
+
article.digest.should be_nil
|
49
|
+
article.valid?
|
50
|
+
article.digest.should == article.generate_digest
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should set the digest value to :digest_column field when :digest_column option is set' do
|
54
|
+
Article.acts_as_digested_on :url, :digest_column => :my_digest
|
55
|
+
|
56
|
+
article = Article.new(:url => @url)
|
57
|
+
article.digest.should be_nil
|
58
|
+
article.my_digest.should be_nil
|
59
|
+
article.valid?
|
60
|
+
article.digest.should be_nil
|
61
|
+
article.my_digest.should == article.generate_digest
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'when validation' do
|
66
|
+
it 'should validate uniqueness of digest field by default' do
|
67
|
+
Article.acts_as_digested_on :url
|
68
|
+
|
69
|
+
Article.create! :url => @url
|
70
|
+
proc { Article.create!(:url => @url) }.should raise_error(ActiveRecord::RecordInvalid)
|
71
|
+
proc { Article.create!(:url => 'http://example2.com') }.should_not raise_error
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should carry over the options to the validates_uniqueness_of options' do
|
75
|
+
Article.acts_as_digested_on :url, :scope => :user_id
|
76
|
+
|
77
|
+
Article.create! :url => @url, :user_id => 1
|
78
|
+
proc { Article.create!(:url => @url, :user_id => 1) }.should raise_error(ActiveRecord::RecordInvalid)
|
79
|
+
proc { Article.create!(:url => @url, :user_id => 2) }.should_not raise_error
|
80
|
+
proc { Article.create!(:url => 'http://example2.com', :user_id => 1) }.should_not raise_error
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should validate uniqueness of digest field when :unique option is true' do
|
84
|
+
Article.acts_as_digested_on :url, :unique => true
|
85
|
+
|
86
|
+
Article.create! :url => @url
|
87
|
+
proc { Article.create!(:url => @url) }.should raise_error(ActiveRecord::RecordInvalid)
|
88
|
+
proc { Article.create!(:url => 'http://example2.com') }.should_not raise_error
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should not validate uniqueness of digest field when :unique option is false' do
|
92
|
+
Article.acts_as_digested_on :url, :unique => false
|
93
|
+
|
94
|
+
Article.create! :url => @url
|
95
|
+
proc { Article.create!(:url => @url) }.should_not raise_error
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should support string key options' do
|
99
|
+
Article.acts_as_digested_on :url, 'unique' => false
|
100
|
+
|
101
|
+
Article.create! :url => @url
|
102
|
+
proc { Article.create!(:url => @url) }.should_not raise_error
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
require 'acts_as_digested_on'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.filter_run :focus => true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
16
|
+
|
17
|
+
class ActiveRecord::Base
|
18
|
+
def self.define_table(table_name = self.name.tableize, &migration)
|
19
|
+
ActiveRecord::Schema.define(:version => 1) do
|
20
|
+
create_table(table_name, &migration)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_digested_on
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- milk1000cc
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-07 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70338358717700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70338358717700
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70338358717280 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70338358717280
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70338358716860 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70338358716860
|
47
|
+
description: This gem sets the digested value before validation and validates uniqueness
|
48
|
+
of the digested value.
|
49
|
+
email:
|
50
|
+
- info@milk1000.cc
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- acts_as_digested_on.gemspec
|
61
|
+
- lib/acts_as_digested_on.rb
|
62
|
+
- lib/acts_as_digested_on/version.rb
|
63
|
+
- spec/acts_as_digested_on_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: https://github.com/milk1000cc/acts_as_digested_on
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project: acts_as_digested_on
|
85
|
+
rubygems_version: 1.8.10
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Automatically set sha-1 hex digest
|
89
|
+
test_files:
|
90
|
+
- spec/acts_as_digested_on_spec.rb
|
91
|
+
- spec/spec_helper.rb
|