laserlemon-belongs_to_versioned 0.1.0

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.
@@ -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.
@@ -0,0 +1,10 @@
1
+ belongs_to_versioned.gemspec
2
+ init.rb
3
+ lib/belongs_to_versioned.rb
4
+ MIT-LICENSE
5
+ Rakefile
6
+ README.rdoc
7
+ test/belongs_to_versioned_test.rb
8
+ test/test_helper.rb
9
+ VERSION.yml
10
+ Manifest
@@ -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>).
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('belongs_to_versioned', '0.1.0') do |g|
6
+ g.description = %(Simplify ActiveRecord associations using acts_as_versioned)
7
+ g.url = 'http://github.com/laserlemon/belongs_to_versioned'
8
+ g.author = 'Steve Richert'
9
+ g.email = 'steve@laserlemon.com'
10
+ g.ignore_pattern = %w(tmp/* script/*)
11
+ g.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|t| load t }
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{belongs_to_versioned}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Steve Richert"]
9
+ s.date = %q{2009-04-28}
10
+ s.description = %q{Simplify ActiveRecord associations using acts_as_versioned}
11
+ s.email = %q{steve@laserlemon.com}
12
+ s.extra_rdoc_files = ["lib/belongs_to_versioned.rb", "README.rdoc"]
13
+ s.files = ["belongs_to_versioned.gemspec", "init.rb", "lib/belongs_to_versioned.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "test/belongs_to_versioned_test.rb", "test/test_helper.rb", "VERSION.yml"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/laserlemon/belongs_to_versioned}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Belongs_to_versioned", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{belongs_to_versioned}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Simplify ActiveRecord associations using acts_as_versioned}
21
+ s.test_files = ["test/belongs_to_versioned_test.rb", "test/test_helper.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ 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)
22
+ when Symbol: (parent.respond_to?(:versions) ? parent.versions.send(revert_to).version : 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,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: laserlemon-belongs_to_versioned
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Richert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simplify ActiveRecord associations using acts_as_versioned
17
+ email: steve@laserlemon.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/belongs_to_versioned.rb
24
+ - README.rdoc
25
+ files:
26
+ - belongs_to_versioned.gemspec
27
+ - init.rb
28
+ - lib/belongs_to_versioned.rb
29
+ - Manifest
30
+ - MIT-LICENSE
31
+ - Rakefile
32
+ - README.rdoc
33
+ - test/belongs_to_versioned_test.rb
34
+ - test/test_helper.rb
35
+ - VERSION.yml
36
+ has_rdoc: true
37
+ homepage: http://github.com/laserlemon/belongs_to_versioned
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - Belongs_to_versioned
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "1.2"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: belongs_to_versioned
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Simplify ActiveRecord associations using acts_as_versioned
67
+ test_files:
68
+ - test/belongs_to_versioned_test.rb
69
+ - test/test_helper.rb