belongs_to_versioned 0.2.2

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
+ .DS_Store
2
+ pkg
3
+ rdoc
4
+ test/*.db
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve Richert
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,88 @@
1
+ = belongs_to_versioned
2
+
3
+ The purpose of <tt>belongs_to_versioned</tt> is to save version information as used by the <tt>acts_as_versioned</tt>[http://github.com/technoweenie/acts_as_versioned/tree/master] plugin by technoweenie[http://github.com/technoweenie] alongside <tt>belongs_to</tt> ActiveRecord associations.
4
+
5
+ == Installation
6
+
7
+ script/plugin install git://github.com/laserlemon/belongs_to_versioned.git
8
+
9
+ == Example
10
+
11
+ In your migrations:
12
+
13
+ create_table :products do |t|
14
+ t.string :name
15
+ t.text :description
16
+ t.integer :price
17
+ t.integer :version
18
+ t.timestamps
19
+ end
20
+ Product.create_versioned_table
21
+
22
+
23
+ create_table :line_items do |t|
24
+ t.belongs_to :product
25
+ t.integer :product_version
26
+ t.integer :quantity
27
+ t.timestamps
28
+ end
29
+
30
+ In your models:
31
+
32
+ class Product < ActiveRecord::Base
33
+ validates_presence_of :name, :description, :price
34
+ validates_numericality_of :price, :greater_than_or_equal_to => 0, :only_integer => true
35
+
36
+ acts_as_versioned :if_changed => [:price]
37
+
38
+ has_many :line_items
39
+ end
40
+
41
+
42
+ class LineItem < ActiveRecord::Base
43
+ validates_presence_of :product, :quantity
44
+ validates_numericality_of :quantity, :greater_than => 0, :only_integer => true
45
+
46
+ belongs_to_versioned :product
47
+
48
+ def subtotal
49
+ product.price * quantity
50
+ end
51
+ end
52
+
53
+ In your controller:
54
+
55
+ product = Product.create(
56
+ :name => 'Settlers of Catan',
57
+ :description => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
58
+ :price => 3999
59
+ )
60
+ @line_items = []
61
+ @line_items << LineItem.create(:product => product, :quantity => 1)
62
+ product.update_attributes(:price => 2999)
63
+ @line_items << LineItem.create(:product => product, :quantity => 1)
64
+
65
+ In your view:
66
+
67
+ <table>
68
+ <tr>
69
+ <th>Quantity</th>
70
+ <th>Product</th>
71
+ <th>Price</th>
72
+ <th>Subtotal</th>
73
+ </tr>
74
+ <%- @line_items.each do |line_item| -%>
75
+ <tr>
76
+ <td><%= line_item.quantity %></td>
77
+ <td><%=h line_item.product.name %></td>
78
+ <td><%= number_to_currency(line_item.product.price.to_f / 100) %></td>
79
+ <td><%= number_to_currency(line_item.subtotal.to_f / 100) %></td>
80
+ </tr>
81
+ <%- end -%>
82
+ </table>
83
+
84
+ == Tips
85
+
86
+ * As an alternative to the <tt>belongs_to_versioned</tt> method, you can pass a <tt>:versioned => true</tt> option to a typical <tt>belongs_to</tt> association.
87
+ * The <tt>:revert_to</tt> option is an optional string specifying the column name to use when reverting the parent object. This defaults to the name of the association appended by <tt>"_version"</tt>.
88
+ * If the <tt>:revert_to</tt> value is a symbol, that <tt>acts_as_versioned</tt> extension method will be used when reverting the parent object (i.e. <tt>:earliest</tt>).
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'belongs_to_versioned'
10
+ g.summary = %(Simplify associations using vestal_versions or acts_as_versioned)
11
+ g.description = %(Simplify associations using vestal_versions or acts_as_versioned)
12
+ g.email = 'steve@laserlemon.com'
13
+ g.homepage = 'http://github.com/laserlemon/belongs_to_versioned'
14
+ g.authors = %w(laserlemon)
15
+ g.rubyforge_project = 'laser-lemon'
16
+ end
17
+ Jeweler::RubyforgeTasks.new do |r|
18
+ r.doc_task = 'rdoc'
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
23
+ end
24
+
25
+ Rake::TestTask.new do |t|
26
+ t.libs = %w(test)
27
+ t.pattern = 'test/**/*_test.rb'
28
+ end
29
+
30
+ task :default => :test
31
+
32
+ Rake::RDocTask.new do |r|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : nil
34
+ r.rdoc_dir = 'rdoc'
35
+ r.title = ['belongs_to_versioned', version].compact.join(' ')
36
+ r.rdoc_files.include('README*')
37
+ r.rdoc_files.include('lib/**/*.rb')
38
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{belongs_to_versioned}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["laserlemon"]
12
+ s.date = %q{2009-10-07}
13
+ s.description = %q{Simplify associations using vestal_versions or acts_as_versioned}
14
+ s.email = %q{steve@laserlemon.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "belongs_to_versioned.gemspec",
26
+ "init.rb",
27
+ "lib/belongs_to_versioned.rb",
28
+ "test/belongs_to_versioned_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/laserlemon/belongs_to_versioned}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubyforge_project = %q{laser-lemon}
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Simplify associations using vestal_versions or acts_as_versioned}
37
+ s.test_files = [
38
+ "test/belongs_to_versioned_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'belongs_to_versioned'
@@ -0,0 +1,45 @@
1
+ module LaserLemon
2
+ module BelongsToVersioned
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.class_eval do
6
+ class << self
7
+ alias_method_chain :belongs_to, :versioned
8
+ end
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def belongs_to_versioned(association_id, options = {})
14
+ revert_to = options.delete(:revert_to) || association_id.to_s.foreign_key.gsub(/_id$/, '_version')
15
+
16
+ belongs_to association_id, options
17
+
18
+ define_method "#{association_id}_with_belongs_to_versioned" do
19
+ parent = send("#{association_id}_without_belongs_to_versioned")
20
+ target_version = case revert_to
21
+ when String: read_attribute(revert_to) rescue nil
22
+ when Symbol: send(revert_to) rescue nil
23
+ end
24
+ parent.revert_to(target_version) if target_version && parent.respond_to?(:revert_to)
25
+ parent
26
+ end
27
+
28
+ alias_method_chain association_id, :belongs_to_versioned
29
+
30
+ define_method "#{association_id}_with_belongs_to_versioned=" do |parent|
31
+ write_attribute(revert_to, (parent.respond_to?(:version) ? parent.version : nil)) if revert_to.is_a?(String)
32
+ send("#{association_id}_without_belongs_to_versioned=", parent)
33
+ end
34
+
35
+ alias_method_chain "#{association_id}=", :belongs_to_versioned
36
+ end
37
+
38
+ def belongs_to_with_versioned(a, o = {})
39
+ o.delete(:versioned) ? belongs_to_versioned(a, o) : belongs_to_without_versioned(a, o)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ ActiveRecord::Base.send(:include, LaserLemon::BelongsToVersioned)
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class BelongsToVersionedTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: belongs_to_versioned
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - laserlemon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-07 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simplify associations using vestal_versions or acts_as_versioned
17
+ email: steve@laserlemon.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - belongs_to_versioned.gemspec
32
+ - init.rb
33
+ - lib/belongs_to_versioned.rb
34
+ - test/belongs_to_versioned_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/laserlemon/belongs_to_versioned
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project: laser-lemon
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Simplify associations using vestal_versions or acts_as_versioned
64
+ test_files:
65
+ - test/belongs_to_versioned_test.rb
66
+ - test/test_helper.rb