Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Alias acts_as_heir_of and acts_as_predecessor #4

Merged
merged 4 commits into from

2 participants

@JeredOdegard

I've aliased acts_as_heir_of and acts_as_predecessor as child_of and parent_model respectively. I think these methods are more clear and simple.

I attempted to alias the methods but was unsuccessful using alias_method :new :old, so I went ahead and just added a method to each module to pass to the existing. This will aid in backwards compatibility. If you know how to successfully alias the method in the module, that would be great and a lot cleaner. You can use the Rspec tests in the heritage_demo to verify your aliasing.

I added Rspec to the test group for the demo to verify the changes are working as intended.

I also modified the Gemfile in the demo to point to the relative location of the Heritage library and removed Test:Unit tests as they were not being utilized.

@dipth dipth merged commit 8acad52 into dipth:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 125 additions and 158 deletions.
  1. +14 −0 Gemfile.lock
  2. +1 −0  heritage_demo/.rspec
  3. +9 −24 heritage_demo/Gemfile
  4. +33 −2 heritage_demo/Gemfile.lock
  5. +1 −1  heritage_demo/app/models/blog_post.rb
  6. +5 −1 heritage_demo/app/models/post.rb
  7. +4 −0 heritage_demo/spec/factories.rb
  8. +17 −0 heritage_demo/spec/models/blog_post_spec.rb
  9. +33 −0 heritage_demo/spec/spec_helper.rb
  10. +0 −9 heritage_demo/test/fixtures/blog_posts.yml
  11. +0 −11 heritage_demo/test/fixtures/categories.yml
  12. +0 −9 heritage_demo/test/fixtures/image_posts.yml
  13. +0 −11 heritage_demo/test/fixtures/posts.yml
  14. +0 −8 heritage_demo/test/functional/blog_posts_controller_test.rb
  15. +0 −8 heritage_demo/test/functional/image_posts_controller_test.rb
  16. +0 −8 heritage_demo/test/functional/posts_controller_test.rb
  17. +0 −9 heritage_demo/test/performance/browsing_test.rb
  18. +0 −13 heritage_demo/test/test_helper.rb
  19. +0 −8 heritage_demo/test/unit/blog_post_test.rb
  20. +0 −8 heritage_demo/test/unit/category_test.rb
  21. +0 −4 heritage_demo/test/unit/helpers/blog_posts_helper_test.rb
  22. +0 −4 heritage_demo/test/unit/helpers/image_posts_helper_test.rb
  23. +0 −4 heritage_demo/test/unit/helpers/posts_helper_test.rb
  24. +0 −8 heritage_demo/test/unit/image_post_test.rb
  25. +0 −8 heritage_demo/test/unit/post_test.rb
  26. +4 −0 lib/heritage/active_record/acts_as_heir.rb
  27. +4 −0 lib/heritage/active_record/acts_as_predecessor.rb
View
14 Gemfile.lock
@@ -0,0 +1,14 @@
+PATH
+ remote: .
+ specs:
+ heritage (0.3.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ heritage!
View
1  heritage_demo/.rspec
@@ -0,0 +1 @@
+--colour
View
33 heritage_demo/Gemfile
@@ -6,27 +6,12 @@ gem 'rails', '3.0.6'
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
-gem "heritage", :path => "~/Projects/benjamin/heritage"
-
-# Use unicorn as the web server
-# gem 'unicorn'
-
-# Deploy with Capistrano
-# gem 'capistrano'
-
-# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
-# gem 'ruby-debug'
-# gem 'ruby-debug19', :require => 'ruby-debug'
-
-# Bundle the extra gems:
-# gem 'bj'
-# gem 'nokogiri'
-# gem 'sqlite3-ruby', :require => 'sqlite3'
-# gem 'aws-s3', :require => 'aws/s3'
-
-# Bundle gems for the local environment. Make sure to
-# put test-only gems in this group so their generators
-# and rake tasks are available in development mode:
-# group :development, :test do
-# gem 'webrat'
-# end
+gem "heritage", :path => '../'
+
+group :test do
+ gem 'rspec-rails'
+ gem 'factory_girl_rails'
+ gem 'autotest'
+ gem 'autotest-fsevent'
+ gem 'autotest-growl'
+end
View
35 heritage_demo/Gemfile.lock
@@ -1,11 +1,12 @@
PATH
- remote: ~/Projects/benjamin/heritage
+ remote: ../
specs:
- heritage (0.3.0)
+ heritage (0.3.1)
GEM
remote: http://rubygems.org/
specs:
+ ZenTest (4.6.2)
abstract (1.0.0)
actionmailer (3.0.6)
actionpack (= 3.0.6)
@@ -34,9 +35,20 @@ GEM
activesupport (= 3.0.6)
activesupport (3.0.6)
arel (2.0.9)
+ autotest (4.4.6)
+ ZenTest (>= 4.4.1)
+ autotest-fsevent (0.2.7)
+ sys-uname
+ autotest-growl (0.2.16)
builder (2.1.2)
+ diff-lcs (1.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
+ factory_girl (2.3.2)
+ activesupport
+ factory_girl_rails (1.4.0)
+ factory_girl (~> 2.3.0)
+ railties (>= 3.0.0)
i18n (0.5.0)
mail (2.2.15)
activesupport (>= 2.3.6)
@@ -64,7 +76,21 @@ GEM
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
+ rspec (2.7.0)
+ rspec-core (~> 2.7.0)
+ rspec-expectations (~> 2.7.0)
+ rspec-mocks (~> 2.7.0)
+ rspec-core (2.7.1)
+ rspec-expectations (2.7.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.7.0)
+ rspec-rails (2.7.0)
+ actionpack (~> 3.0)
+ activesupport (~> 3.0)
+ railties (~> 3.0)
+ rspec (~> 2.7.0)
sqlite3 (1.3.3)
+ sys-uname (0.8.6)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
@@ -74,6 +100,11 @@ PLATFORMS
ruby
DEPENDENCIES
+ autotest
+ autotest-fsevent
+ autotest-growl
+ factory_girl_rails
heritage!
rails (= 3.0.6)
+ rspec-rails
sqlite3
View
2  heritage_demo/app/models/blog_post.rb
@@ -1,6 +1,6 @@
class BlogPost < ActiveRecord::Base
- acts_as_heir_of :post
+ child_of :post
validates_presence_of :body
validates_presence_of :title
View
6 heritage_demo/app/models/post.rb
@@ -1,6 +1,6 @@
class Post < ActiveRecord::Base
- acts_as_predecessor :exposes => :hello
+ parent_model :exposes => :hello
belongs_to :category
@@ -8,4 +8,8 @@ def hello
"Hi there!"
end
+ def hidden_hello
+ "Oh no!"
+ end
+
end
View
4 heritage_demo/spec/factories.rb
@@ -0,0 +1,4 @@
+Factory.define :blog_post do |blog_post|
+ blog_post.body "Hey there!"
+ blog_post.title "Hello World"
+end
View
17 heritage_demo/spec/models/blog_post_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe BlogPost do
+ let(:blog_post) { Factory(:blog_post) }
+
+ it "should inherit attributes" do
+ blog_post.should respond_to :title
+ end
+
+ it "should inherit exposed methods" do
+ blog_post.should respond_to :hello
+ end
+
+ it "should not inherit non-exposed methods" do
+ blog_post.should_not respond_to :hidden_hello
+ end
+end
View
33 heritage_demo/spec/spec_helper.rb
@@ -0,0 +1,33 @@
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+ENV["RAILS_ENV"] ||= 'test'
+require File.expand_path("../../config/environment", __FILE__)
+require 'rspec/rails'
+require 'rspec/autorun'
+
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+RSpec.configure do |config|
+ # == Mock Framework
+ #
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
+ #
+ # config.mock_with :mocha
+ # config.mock_with :flexmock
+ # config.mock_with :rr
+ config.mock_with :rspec
+
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # If true, the base class of anonymous controllers will be inferred
+ # automatically. This will be the default behavior in future versions of
+ # rspec-rails.
+ config.infer_base_class_for_anonymous_controllers = false
+end
View
9 heritage_demo/test/fixtures/blog_posts.yml
@@ -1,9 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- predecessor_id: 1
- body: MyText
-
-two:
- predecessor_id: 1
- body: MyText
View
11 heritage_demo/test/fixtures/categories.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-# This model initially had no columns defined. If you add columns to the
-# model remove the '{}' from the fixture names and add the columns immediately
-# below each fixture, per the syntax in the comments below
-#
-one: {}
-# column: value
-#
-two: {}
-# column: value
View
9 heritage_demo/test/fixtures/image_posts.yml
@@ -1,9 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- predecessor_id: 1
- aspect: 1
-
-two:
- predecessor_id: 1
- aspect: 1
View
11 heritage_demo/test/fixtures/posts.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- heir_id: 1
- heir_type: MyString
- title: MyString
-
-two:
- heir_id: 1
- heir_type: MyString
- title: MyString
View
8 heritage_demo/test/functional/blog_posts_controller_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class BlogPostsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
8 heritage_demo/test/functional/image_posts_controller_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class ImagePostsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
8 heritage_demo/test/functional/posts_controller_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class PostsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
9 heritage_demo/test/performance/browsing_test.rb
@@ -1,9 +0,0 @@
-require 'test_helper'
-require 'rails/performance_test_help'
-
-# Profiling results for each test method are written to tmp/performance.
-class BrowsingTest < ActionDispatch::PerformanceTest
- def test_homepage
- get '/'
- end
-end
View
13 heritage_demo/test/test_helper.rb
@@ -1,13 +0,0 @@
-ENV["RAILS_ENV"] = "test"
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/test_help'
-
-class ActiveSupport::TestCase
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
- fixtures :all
-
- # Add more helper methods to be used by all tests here...
-end
View
8 heritage_demo/test/unit/blog_post_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class BlogPostTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
8 heritage_demo/test/unit/category_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class CategoryTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
4 heritage_demo/test/unit/helpers/blog_posts_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class BlogPostsHelperTest < ActionView::TestCase
-end
View
4 heritage_demo/test/unit/helpers/image_posts_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class ImagePostsHelperTest < ActionView::TestCase
-end
View
4 heritage_demo/test/unit/helpers/posts_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class PostsHelperTest < ActionView::TestCase
-end
View
8 heritage_demo/test/unit/image_post_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class ImagePostTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
8 heritage_demo/test/unit/post_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class PostTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
View
4 lib/heritage/active_record/acts_as_heir.rb
@@ -1,6 +1,10 @@
module Heritage
module ActiveRecord
module ActsAsHeir
+
+ def child_of(parent_symbol)
+ acts_as_heir_of(parent_symbol)
+ end
def acts_as_heir_of(predecessor_symbol)
extend ClassMethods
View
4 lib/heritage/active_record/acts_as_predecessor.rb
@@ -1,6 +1,10 @@
module Heritage
module ActiveRecord
module ActsAsPredecessor
+
+ def parent_model(options = {})
+ acts_as_predecessor(options)
+ end
def acts_as_predecessor(options = {})
extend ClassMethods
Something went wrong with that request. Please try again.