changes 0.0.1

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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in changes.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,6 @@
1
+ module Changes
2
+ class ResourceChange < ActiveRecord::Base
3
+ belongs_to :logged_resource, :polymorphic => true
4
+
5
+ end
6
+ end
data/changes.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "changes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "changes"
7
+ s.version = Changes::VERSION
8
+ s.authors = ["Jesper Christiansen"]
9
+ s.email = ["jesper@creativegenius.dk"]
10
+ s.homepage = "http://www.github.com/jespr/changes"
11
+ s.summary = %q{Adds logging of changes to your model}
12
+ s.description = %q{Adds logging of changes to your model, so you can see a list of changes - and what the values were changed from}
13
+
14
+ s.rubyforge_project = "changes"
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
+ end
@@ -0,0 +1,3 @@
1
+ module Changes
2
+ VERSION = "0.0.1"
3
+ end
data/lib/changes.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "changes/version"
2
+
3
+ module Changes
4
+ require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
5
+ require 'record_changes/base'
6
+ end
data/lib/engine.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'changes'
2
+ require 'rails'
3
+
4
+ module Changes
5
+ class Engine < Rails::Engine
6
+ # Check the gem config
7
+ initializer "check config" do |app|
8
+
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ChangesGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ def self.source_root
7
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
8
+ end
9
+
10
+ def self.next_migration_number(dirname)
11
+ if ActiveRecord::Base.timestamped_migrations
12
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
13
+ else
14
+ "%.3d" % (current_migration_number(dirname) + 1)
15
+ end
16
+ end
17
+
18
+ def create_migration_file
19
+ migration_template 'migration.rb', 'db/migrate/create_changes_table.rb'
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ class CreateChangesTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :changes, :force => true do |t|
4
+ t.text :log
5
+ t.string :logged_resource_type
6
+ t.integer :logged_resource_id
7
+ t.datetime :created_at
8
+ t.datetime :updated_at
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :changes
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ module Changes
2
+ module RecordChanges
3
+
4
+ ## Define ModelMethods
5
+ module Base
6
+ def self.included(klass)
7
+ klass.class_eval do
8
+ extend Config
9
+ end
10
+ end
11
+
12
+ module Config
13
+ def record_changes
14
+
15
+ # This is where arbitrary code goes that you want to
16
+ # add to the class that declared "acts_as_widget"
17
+
18
+ has_many :resource_changes, :as => :logged_resource, :class_name => 'Changes::ResourceChange'
19
+ before_save :identify_changes
20
+
21
+ include Changes::RecordChanges::Base::InstanceMethods
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+
27
+ def identify_changes
28
+ unless self.new_record? && self.changed.empty?
29
+ self.resource_changes.create(:log_changed => self.changed.to_json, :log_changes => self.changes.to_json)
30
+ end
31
+ end
32
+
33
+ def show_changes(options={})
34
+ all_changes = []
35
+ self.resource_changes.order('created_at').each do |change|
36
+ log = ""
37
+ log_changed = JSON.parse(change.log_changed)
38
+ log_changes = JSON.parse(change.log_changes)
39
+
40
+ log_changed.each do |c|
41
+ from = log_changes[c].first
42
+ to = log_changes[c].last
43
+ log << "#{c} from #{from} to #{to}\n"
44
+ end
45
+ all_changes << log
46
+ end
47
+ all_changes
48
+ end
49
+ end # InstanceMethods
50
+ end
51
+
52
+ end
53
+ end
54
+
55
+ ::ActiveRecord::Base.send :include, Changes::RecordChanges::Base
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: changes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jesper Christiansen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-23 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Adds logging of changes to your model, so you can see a list of changes - and what the values were changed from
22
+ email:
23
+ - jesper@creativegenius.dk
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - app/models/changes/resource_change.rb
35
+ - changes.gemspec
36
+ - lib/changes.rb
37
+ - lib/changes/version.rb
38
+ - lib/engine.rb
39
+ - lib/rails/generators/changes/changes_generator.rb
40
+ - lib/rails/generators/changes/templates/migration.rb
41
+ - lib/record_changes/base.rb
42
+ homepage: http://www.github.com/jespr/changes
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: changes
71
+ rubygems_version: 1.8.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Adds logging of changes to your model
75
+ test_files: []
76
+