acts_as_aliased 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord", ">= 3.0.0"
4
+
5
+ group :development do
6
+ gem "rdoc"
7
+ gem "bundler"
8
+ gem "jeweler"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.3)
5
+ activesupport (= 3.2.3)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.3)
8
+ activemodel (= 3.2.3)
9
+ activesupport (= 3.2.3)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.3)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ arel (3.0.2)
16
+ builder (3.0.0)
17
+ diff-lcs (1.1.3)
18
+ git (1.2.5)
19
+ i18n (0.6.0)
20
+ jeweler (1.8.3)
21
+ bundler (~> 1.0)
22
+ git (>= 1.2.5)
23
+ rake
24
+ rdoc
25
+ json (1.7.3)
26
+ multi_json (1.3.5)
27
+ rake (0.9.2.2)
28
+ rdoc (3.12)
29
+ json (~> 1.4)
30
+ rspec (2.9.0)
31
+ rspec-core (~> 2.9.0)
32
+ rspec-expectations (~> 2.9.0)
33
+ rspec-mocks (~> 2.9.0)
34
+ rspec-core (2.9.0)
35
+ rspec-expectations (2.9.1)
36
+ diff-lcs (~> 1.1.3)
37
+ rspec-mocks (2.9.0)
38
+ sqlite3 (1.3.6)
39
+ tzinfo (0.3.33)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activerecord (>= 3.0.0)
46
+ bundler
47
+ jeweler
48
+ rdoc
49
+ rspec
50
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Thilo Rusche
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.md ADDED
@@ -0,0 +1,87 @@
1
+ acts_as_aliased
2
+ ===============
3
+
4
+ Extends `ActiveRecord::Base` with a mechanism to create aliases for resources.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem 'acts_as_aliased'
10
+
11
+ After updating your bundle, run
12
+
13
+ rails generate acts_as_aliased:install
14
+ rake db:migrate
15
+
16
+ This will create a new table `aliases`.
17
+
18
+ Usage
19
+ -----
20
+
21
+ Let's say you have a model `Company` that requires aliasing because there are different versions of the company name. Enable
22
+ aliasing in your model by using `acts_as_aliased`:
23
+
24
+ model Company < ActiveRecord::Base
25
+ acts_as_aliased
26
+ end
27
+
28
+ This assumes a column called `name` on your company model. You can specify a different column by passing a `column` argument:
29
+
30
+ model Company < ActiveRecord::Base
31
+ acts_as_aliased :column => 'title'
32
+ end
33
+
34
+ An alias can be created manually like this:
35
+
36
+ company = Company.create(name: "foo")
37
+ ActsAsAliased.create(name: "bar", aliased: company)
38
+
39
+ But a more common use case is that you have two instances of a model and would like to convert one into an alias of the other. Say
40
+ you have these two companies:
41
+
42
+ foo = Company.create(name: "Foo")
43
+ foollc = Company.create(name: "Foo LLC")
44
+
45
+ Then you can convert one into an alias for the other like this:
46
+
47
+ foo.to_alias!(foollc) # this will DESTROY foo and create a new alias for foollc in it's place
48
+
49
+ Now let's say you also have a model called `Project` and that a company has many projects:
50
+
51
+ model Company < ActiveRecord::Base
52
+ has_many :projects
53
+ end
54
+
55
+ This implies a foreign key `company_id` in the `projects` table. When you convert a company into
56
+ an alias, you'll also want to update those foreign keys. You can accomplish this by passing the associations
57
+ to be updated to `acts_as_alias`:
58
+
59
+ model Company < ActiveRecord::Base
60
+ has_many :projects
61
+ acts_as_aliased associations: [:projects]
62
+ end
63
+
64
+ Finally, to find a model by it's alias, acts_as_aliased implements a `lookup` class method on your model:
65
+
66
+ company = Company.create(name: "foo")
67
+ ActsAsAliased.create(name: "bar", aliased: company)
68
+
69
+ Company.lookup("foo") # returns company
70
+ Company.lookup("bar") # also returns company
71
+
72
+
73
+ ### Contributing to acts_as_aliased
74
+
75
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
76
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
77
+ * Fork the project.
78
+ * Start a feature/bugfix branch.
79
+ * Commit and push until you are happy with your contribution.
80
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
81
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
82
+
83
+ ### Copyright
84
+
85
+ Copyright (c) 2012 LegitScript. See LICENSE.txt for
86
+ further details.
87
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "acts_as_aliased"
18
+ gem.homepage = "http://github.com/trusche/acts_as_aliased"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Alias names for your models}
21
+ gem.description = %Q{Provides aliases and lookup methods for ActiveRecord models.}
22
+ gem.email = "thilo.rusche@legitscript.com"
23
+ gem.authors = ["Thilo Rusche"]
24
+ gem.version = "0.0.4"
25
+ gem.files = FileList["[A-Z]*", "{lib,spec}/**/*"]
26
+ gem.require_paths = ['lib']
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "acts_as_aliased_2 #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_record'
2
+
3
+ module ActsAsAliased
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ class Alias < ActiveRecord::Base
9
+ belongs_to :aliased, polymorphic: true
10
+ attr_accessible :aliased, :name
11
+ end
12
+
13
+ module ClassMethods
14
+ def acts_as_aliased options = {}
15
+ has_many :aliases, as: :aliased, class_name: ::ActsAsAliased::Alias
16
+
17
+ cattr_accessor :associations
18
+ cattr_accessor :column
19
+ self.associations = options[:associations] || []
20
+ self.column = options[:column] || 'name'
21
+
22
+ class_eval <<-EOV
23
+ include ActsAsAliased::InstanceMethods
24
+
25
+ def self.lookup(value)
26
+ return nil if value.blank?
27
+ self.send("find_by_#{column}", value) ||
28
+ Alias.where(["aliased_type = ? AND name = ?", self.to_s, value]).first.try(:aliased)
29
+ end
30
+ EOV
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ def to_alias! aliased
36
+ raise "Cannot create alias for myself" if aliased == self
37
+ self.class.transaction do
38
+ a = ::ActsAsAliased::Alias.create(aliased: aliased, name: self[column])
39
+ associations.each do |association|
40
+ klass = association.to_s.classify.constantize
41
+ key = self.class.to_s.foreign_key
42
+ klass.where(key => id).update_all(key => aliased.id)
43
+ end
44
+ self.destroy
45
+ a
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ ActiveSupport.on_load(:active_record) do
52
+ include ActsAsAliased
53
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ module ActsAsAliased
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ extend ActiveRecord::Generators::Migration
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def create_migration_file
12
+ puts "Adding a migration..."
13
+ migration_template 'migration.rb', 'db/migrate/create_acts_as_alias_table.rb' rescue puts $!.message
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ class CreatesActsAsAliasedTable < ActiveRecord::Migration
2
+ def change
3
+ create_table :aliases do |t|
4
+ t.integer :aliased_id, null: false
5
+ t.string :aliased_type, null: false
6
+ t.string :name, null: false
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :aliases, [:aliased_id, :aliased_type]
11
+ add_index :aliases, :name
12
+ end
13
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActsAsAliased" do
4
+
5
+ let(:company1) { Company.create(name: "Foo LLC") }
6
+ let(:company2) { Company.create(name: "Foo") }
7
+ let(:department1) { Department.create(title: "Technical Staff") }
8
+ let(:department2) { Department.create(title: "Techies") }
9
+ let(:project1) { Project.create(name: "Project 1", company: company1, department: department1) }
10
+ let(:project2) { Project.create(name: "Project 2", company: company2, department: department2) }
11
+ let(:project3) { Project.create(name: "Project 3", company: company2, department: department2) }
12
+
13
+
14
+ context "creating an alias" do
15
+ before { clean_database }
16
+
17
+ it "should work" do
18
+ expect {
19
+ al = ActsAsAliased::Alias.create(name: "Foo", aliased: company1)
20
+ al.aliased_id.should == company1.id
21
+ al.aliased_type.should == 'Company'
22
+ al.name.should == "Foo"
23
+ }.to change{ ActsAsAliased::Alias.count }.by(1)
24
+ end
25
+ end
26
+
27
+ context "converting to alias" do
28
+ before { clean_database }
29
+
30
+ it "should create a new alias" do
31
+ expect {
32
+ al = company2.to_alias!(company1)
33
+ al.should be_an(ActsAsAliased::Alias)
34
+ al.aliased.should == company1
35
+ al.name.should == "Foo"
36
+ }.to change{ ActsAsAliased::Alias.count }.by(1)
37
+ end
38
+
39
+ it "should destroy the resource" do
40
+ company1
41
+ company2
42
+ expect {
43
+ company2.to_alias!(company1)
44
+ }.to change{ Company.count }.by(-1)
45
+ end
46
+
47
+ it "should raise an exception on self" do
48
+ lambda { company1.to_alias!(company1) }.should raise_error
49
+ end
50
+
51
+ context "with associations" do
52
+ before(:each) do
53
+ clean_database
54
+ project1
55
+ project2
56
+ project3
57
+ end
58
+
59
+ it "should reassign associations" do
60
+ company2.to_alias!(company1)
61
+ company1.reload.should have(3).projects
62
+ end
63
+ end
64
+
65
+ context "with a different column name" do
66
+ before(:each) do
67
+ clean_database
68
+ department2.to_alias!(department1)
69
+ end
70
+
71
+ it "should work" do
72
+ Department.lookup("Techies").should == department1
73
+ end
74
+ end
75
+ end
76
+
77
+ context "lookup" do
78
+ before(:each) do
79
+ clean_database
80
+ company2.to_alias!(company1)
81
+ end
82
+
83
+ it "should find by aliased name" do
84
+ Company.lookup("Foo LLC").should == company1
85
+ end
86
+
87
+ it "should find by alias name" do
88
+ Company.lookup("Foo").should == company1
89
+ end
90
+
91
+ it "should return nil if not found" do
92
+ Company.lookup("Bar").should be_nil
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,71 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'acts_as_aliased'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+
14
+
15
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
16
+
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+
19
+ create_table :aliases do |t|
20
+ t.integer :aliased_id, null: false
21
+ t.string :aliased_type, null: false
22
+ t.string :name, null: false
23
+ t.timestamps
24
+ end
25
+
26
+ add_index :aliases, [:aliased_id, :aliased_type]
27
+ add_index :aliases, :name
28
+
29
+
30
+ create_table :companies do |t|
31
+ t.string :name, null: false
32
+ end
33
+
34
+ create_table :departments do |t|
35
+ t.string :title, null: false
36
+ end
37
+
38
+ create_table :projects do |t|
39
+ t.string :name, null: false
40
+ t.integer :company_id, null: false
41
+ t.integer :department_id, null: false
42
+ end
43
+ add_index :projects, :company_id
44
+ add_index :projects, :department_id
45
+ end
46
+
47
+ class Project < ActiveRecord::Base
48
+ belongs_to :company
49
+ belongs_to :department
50
+ end
51
+
52
+ class Company < ActiveRecord::Base
53
+ has_many :projects
54
+ acts_as_aliased associations: [:projects]
55
+ end
56
+
57
+ class Department < ActiveRecord::Base
58
+ has_many :projects
59
+ acts_as_aliased column: 'title', associations: [:projects]
60
+ end
61
+
62
+
63
+ def clean_database
64
+ models = [ActsAsAliased::Alias, Project, Company, Department]
65
+ models.each do |model|
66
+ ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
67
+ end
68
+ end
69
+
70
+
71
+
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_aliased
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thilo Rusche
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Provides aliases and lookup methods for ActiveRecord models.
111
+ email: thilo.rusche@legitscript.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.md
117
+ files:
118
+ - Gemfile
119
+ - Gemfile.lock
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/acts_as_aliased.rb
124
+ - lib/generators/acts_as_aliased/install_generator.rb
125
+ - lib/generators/acts_as_aliased/templates/migration.rb
126
+ - spec/acts_as_aliased_spec.rb
127
+ - spec/spec_helper.rb
128
+ homepage: http://github.com/trusche/acts_as_aliased
129
+ licenses:
130
+ - MIT
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ segments:
142
+ - 0
143
+ hash: -3303482588023467799
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.21
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: Alias names for your models
156
+ test_files: []