ku_fu 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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ku_fu.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ku_fu.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ku_fu/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ku_fu"
7
+ s.version = KuFu::VERSION
8
+ s.authors = ["Andrea Campolonghi"]
9
+ s.email = ["acampolonghi@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = "register who created, updated or deleted an active record object"
12
+ s.description = "register who created, updated or deleted an active record object"
13
+
14
+ s.rubyforge_project = "ku_fu"
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 "activerecord"
24
+ end
data/lib/ku_fu/base.rb ADDED
@@ -0,0 +1,28 @@
1
+ module KuFu
2
+
3
+ def self.included base
4
+ base.class_eval do
5
+ extend ClassMethods
6
+ include InstanceMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def register_ku_fu
12
+ before_save :ku_fu_register
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ def ku_fu_register
18
+ if new_record?
19
+ write_attribute(:creator_id, User.current.id) if self.respond_to?(:creator_id)
20
+ else
21
+ write_attribute(:updater_id, User.current.id) if self.respond_to?(:updater_id)
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ ActiveRecord::Base.send(:include, KuFu)
@@ -0,0 +1,3 @@
1
+ module KuFu
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ku_fu.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "ku_fu/version"
2
+ require "ku_fu/base"
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe "ku_fu" do
4
+
5
+ it "should verify that User.current is not nil" do
6
+ User.current.should_not be_nil
7
+ end
8
+
9
+ context "given a class that register_ku_fu" do
10
+
11
+ before do
12
+ @subject = Document
13
+ end
14
+
15
+ context "when creating" do
16
+
17
+ it "should save the User.current id as creator_id" do
18
+ model = @subject.create!
19
+ model.creator_id.should_not be_nil
20
+ model.creator_id.should eq User.current.id
21
+ end
22
+
23
+ end
24
+
25
+ context "when updating" do
26
+
27
+ it "should save the User.current id as creator_id" do
28
+ model = @subject.create!
29
+ new_user = User.create!
30
+ User.current = new_user
31
+ model.save!
32
+ model.updater_id.should eq User.current.id
33
+ model.creator_id.should_not eq model.updater_id
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'sqlite3'
4
+
5
+ require 'ku_fu'
6
+
7
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
8
+ FileUtils.mkdir_p File.dirname(__FILE__) + '/../db/'
9
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => "db/test.sqlite3"
10
+
11
+ RSpec.configure do |config|
12
+ config.mock_with :rspec
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ config.before :suite do
18
+ ActiveRecord::Base.connection.create_table(:users,:force => true) do |t|
19
+ t.timestamps
20
+ end
21
+ ActiveRecord::Base.connection.create_table(:documents,:force => true) do |t|
22
+ t.string :title
23
+ t.integer :creator_id
24
+ t.integer :updater_id
25
+ t.timestamps
26
+ end
27
+ User.current = User.create!
28
+ end
29
+
30
+ config.before(:each) do
31
+ Document.delete_all
32
+ end
33
+
34
+ config.after(:suite) do
35
+ ActiveRecord::Base.connection.drop_table(:users)
36
+ ActiveRecord::Base.connection.drop_table(:documents)
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ class Document < ActiveRecord::Base
2
+ register_ku_fu
3
+ end
4
+
@@ -0,0 +1,12 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ class << self
4
+ def current
5
+ @current
6
+ end
7
+ def current=user
8
+ @current = user
9
+ end
10
+ end
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ku_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrea Campolonghi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70275885299360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70275885299360
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70275885298800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70275885298800
36
+ description: register who created, updated or deleted an active record object
37
+ email:
38
+ - acampolonghi@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - Rakefile
47
+ - ku_fu.gemspec
48
+ - lib/ku_fu.rb
49
+ - lib/ku_fu/base.rb
50
+ - lib/ku_fu/version.rb
51
+ - spec/ku_fu_spec.rb
52
+ - spec/spec_helper.rb
53
+ - spec/support/document.rb
54
+ - spec/support/user.rb
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project: ku_fu
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: register who created, updated or deleted an active record object
79
+ test_files:
80
+ - spec/ku_fu_spec.rb
81
+ - spec/spec_helper.rb
82
+ - spec/support/document.rb
83
+ - spec/support/user.rb