fingerprints 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rails', '> 3.2.0'
5
+ gem 'sqlite3'
6
+ end
7
+
8
+ # Specify your gem's dependencies in fingerprints.gemspec
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philip Hallstrom
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,70 @@
1
+ # Fingerprints
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/fingerprints.png)](http://badge.fury.io/rb/fingerprints)
4
+
5
+ Make it easy to track who created/updated your models.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'fingerprints'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fingerprints
20
+
21
+ ## Usage
22
+
23
+
24
+ Example
25
+ =======
26
+
27
+ # Widget Schema...
28
+ create_table :widgets do |t|
29
+ t.string :name
30
+ t.fingerprints # creates integer fields for created_by and updated_by
31
+ end
32
+
33
+ # User model...
34
+ class User < ActiveRecord::Base
35
+ has_fingerprints
36
+ end
37
+
38
+ # Widget model...
39
+ class Widget < ActiveRecord::Base
40
+ leaves_fingerprints
41
+
42
+ # If your 'user' is really a Person you could do this:
43
+ #
44
+ # leaves_fingerprints :class_name => 'Person'
45
+ end
46
+
47
+ Now, some how, some way you need to set User.fingerprint to either the User instance
48
+ or User 'id' of the "currently logged in user". One way to do this would be to put
49
+ this in your controller assuming your controller has a `:current_user` method that will
50
+ return the current user.
51
+
52
+ before_filter { |c| User.fingerprint = c.send(:current_user) }
53
+
54
+ At this point if you create/update a Widget it will set the `created_by/updated_by` attributes
55
+ automatically. You can also do this:
56
+
57
+ @widget.creator => User instance...
58
+ @widget.updator => User instance...
59
+
60
+ The default `:class_name` is 'User' and can be overridden like this:
61
+
62
+ ActiveRecord::HasFingerprints::OPTIONS.merge!(:class_name => 'Person')
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib'
6
+ test.libs << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fingerprints/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fingerprints"
8
+ gem.version = Fingerprints::VERSION
9
+ gem.authors = ["Philip Hallstrom"]
10
+ gem.email = ["philip@pjkh.com"]
11
+ gem.description = %q{Make it easy to track who created/updated your models.}
12
+ gem.summary = %q{Make it easy to track who created/updated your models.}
13
+ gem.homepage = "https://github.com/phallstrom/fingerprints"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class TableDefinition
4
+ # Appends <tt>:integer</tt> columns <tt>:created_by</tt> and
5
+ # <tt>:updated_by</tt> to the table.
6
+ def fingerprints(*args)
7
+ options = args.extract_options!
8
+ column(:created_by, :integer, options)
9
+ column(:updated_by, :integer, options)
10
+ end
11
+ end
12
+
13
+ class Table
14
+ # Adds fingerprints (created_by and updated_by) columns to the table. See SchemaStatements#add_fingerprints
15
+ # ===== Example
16
+ # t.fingerprints
17
+ def fingerprints
18
+ @base.add_fingerprints(@table_name)
19
+ end
20
+
21
+
22
+ # Removes the fingerprint columns (created_by and updated_by) from the table.
23
+ # ===== Example
24
+ # t.remove_fingerprints
25
+ def remove_fingerprints
26
+ @base.remove_fingerprints(@table_name)
27
+ end
28
+ end
29
+ end
30
+
31
+ module SchemaStatements
32
+ # Adds fingerprints (created_by and updated_by) columns to the named table.
33
+ # ===== Examples
34
+ # add_fingerprints(:suppliers)
35
+ def add_fingerprints(table_name)
36
+ add_column table_name, :created_by, :integer
37
+ add_column table_name, :updated_by, :integer
38
+ end
39
+
40
+ # Removes the fingerprint columns (created_by and updated_by) from the table definition.
41
+ # ===== Examples
42
+ # remove_fingerprints(:suppliers)
43
+ def remove_fingerprints(table_name)
44
+ remove_column table_name, :updated_by
45
+ remove_column table_name, :created_by
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,3 @@
1
+ module Fingerprints
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'active_record'
2
+ require 'fingerprints/active_record'
3
+ require 'fingerprints/version'
4
+
5
+ module Fingerprints
6
+ module Extensions
7
+
8
+ OPTIONS = {
9
+ :class_name => 'User'
10
+ }
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ def has_fingerprints(options = {})
19
+ options.reverse_merge!(OPTIONS)
20
+
21
+ class_eval <<-"EOV"
22
+ class << self
23
+ def fingerprint
24
+ Thread.current["fingerprint_for_#{self.class}"]
25
+ end
26
+ def fingerprint=(val)
27
+ Thread.current["fingerprint_for_#{self.class}"] = val
28
+ end
29
+ end
30
+ EOV
31
+ end
32
+
33
+ def leaves_fingerprints(options = {})
34
+ options.reverse_merge!(OPTIONS)
35
+
36
+ include Fingerprints::Extensions::InstanceMethods
37
+
38
+ belongs_to :creator, :class_name => options[:class_name], :foreign_key => 'created_by'
39
+ belongs_to :updater, :class_name => options[:class_name], :foreign_key => 'updated_by'
40
+ before_create :fingerprint_created_by
41
+ before_update :fingerprint_updated_by
42
+ define_method('fingerprint_created_by') {|*args| set_fingerprint_for(:created_by, options) }
43
+ define_method('fingerprint_updated_by') {|*args| set_fingerprint_for(:updated_by, options) }
44
+ end
45
+ end
46
+
47
+ module InstanceMethods
48
+ def set_fingerprint_for(field, options = {})
49
+ klass = options[:class_name].constantize
50
+ raise(NoMethodError, "HasFingerprints for #{self.class} expected #{options[:class_name]} to respond to :fingerprint") unless klass.respond_to? :fingerprint
51
+ value = klass.fingerprint
52
+ value = value.id if value.is_a? klass
53
+ self.send("#{field}=", value)
54
+ end
55
+ protected :set_fingerprint_for
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ ActiveRecord::Base.send :include, Fingerprints::Extensions
@@ -0,0 +1,92 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'fingerprints'
5
+
6
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
7
+
8
+ def setup_db
9
+ silence_stream(STDOUT) do
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :widgets do |t|
12
+ t.column :name, :string
13
+ t.fingerprints
14
+ end
15
+ create_table :users do |t|
16
+ t.column :login, :string
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def teardown_db
23
+ silence_stream(STDOUT) do
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
27
+ end
28
+ end
29
+
30
+ class User < ActiveRecord::Base
31
+ has_fingerprints
32
+ def self.table_name() "users" end
33
+ end
34
+
35
+ class InvalidUser < ActiveRecord::Base
36
+ def self.table_name() "users" end
37
+ end
38
+
39
+ class Widget < ActiveRecord::Base
40
+ leaves_fingerprints :class_name => 'User'
41
+ def self.table_name() "widgets" end
42
+ end
43
+
44
+ class InvalidWidget < ActiveRecord::Base
45
+ leaves_fingerprints :class_name => 'InvalidUser'
46
+ def self.table_name() "widgets" end
47
+ end
48
+
49
+ class HasFingerprintsTest < ActiveSupport::TestCase
50
+ def setup
51
+ setup_db
52
+ @widget = Widget.new(:name => 'Spring')
53
+ @invalid_widget = InvalidWidget.new(:name => 'Mobius Strip')
54
+ end
55
+
56
+ def teardown
57
+ teardown_db
58
+ end
59
+
60
+ def test_fingerprint_columns_exist
61
+ assert Widget.column_names.include? 'created_by'
62
+ assert Widget.column_names.include? 'updated_by'
63
+ end
64
+
65
+ def test_fingerprint_associations_exist
66
+ assert_equal :belongs_to, Widget.reflect_on_association(:creator).try(:macro)
67
+ assert_equal :belongs_to, Widget.reflect_on_association(:updater).try(:macro)
68
+ end
69
+
70
+ def test_fingerprints_set_on_create
71
+ User.fingerprint = 5
72
+ @widget.save!
73
+ assert_equal 5, @widget.created_by
74
+ assert_nil @widget.updated_by
75
+ end
76
+
77
+ def test_fingerprints_set_on_update
78
+ User.fingerprint = 5
79
+ @widget.save!
80
+ User.fingerprint = 6
81
+ @widget.save!
82
+ assert_equal 5, @widget.created_by
83
+ assert_equal 6, @widget.updated_by
84
+ end
85
+
86
+ def test_invalid_fingerprint_method_raises_error
87
+ assert_raise NoMethodError do
88
+ @invalid_widget.save
89
+ end
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fingerprints
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Hallstrom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Make it easy to track who created/updated your models.
15
+ email:
16
+ - philip@pjkh.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - fingerprints.gemspec
27
+ - lib/fingerprints.rb
28
+ - lib/fingerprints/active_record.rb
29
+ - lib/fingerprints/version.rb
30
+ - test/test_fingerprints.rb
31
+ homepage: https://github.com/phallstrom/fingerprints
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ segments:
44
+ - 0
45
+ hash: -3235234909104547132
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ segments:
53
+ - 0
54
+ hash: -3235234909104547132
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Make it easy to track who created/updated your models.
61
+ test_files:
62
+ - test/test_fingerprints.rb