has_attributes_from 0.1.2
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/History.txt +7 -0
- data/Manifest.txt +32 -0
- data/README.textile +96 -0
- data/Rakefile +1 -0
- data/lib/has_attributes_from.rb +6 -0
- data/lib/has_attributes_from/base.rb +74 -0
- data/tasks/has_attributes_from.rake +33 -0
- data/test/factories/general.rb +28 -0
- data/test/rails_root/app/models/customer.rb +6 -0
- data/test/rails_root/app/models/customer_detail.rb +2 -0
- data/test/rails_root/app/models/employee.rb +3 -0
- data/test/rails_root/app/models/employee_property.rb +2 -0
- data/test/rails_root/app/models/manager.rb +2 -0
- data/test/rails_root/app/models/person.rb +2 -0
- data/test/rails_root/config/boot.rb +110 -0
- data/test/rails_root/config/database.yml +11 -0
- data/test/rails_root/config/environment.rb +12 -0
- data/test/rails_root/config/environments/test.rb +0 -0
- data/test/rails_root/config/routes.rb +0 -0
- data/test/rails_root/db/migrate/20090820105348_create_people.rb +15 -0
- data/test/rails_root/db/migrate/20090820105521_create_employee_properties.rb +15 -0
- data/test/rails_root/db/migrate/20090820110621_create_customer_details.rb +14 -0
- data/test/rails_root/db/schema.rb +37 -0
- data/test/rails_root/db/test.sqlite3 +0 -0
- data/test/test_helper.rb +15 -0
- data/test/unit/customer_detail_test.rb +13 -0
- data/test/unit/customer_test.rb +70 -0
- data/test/unit/employee_property_test.rb +13 -0
- data/test/unit/employee_test.rb +48 -0
- data/test/unit/manager_test.rb +13 -0
- data/test/unit/person_test.rb +13 -0
- metadata +85 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.textile
|
4
|
+
Rakefile
|
5
|
+
lib/has_attributes_from/base.rb
|
6
|
+
lib/has_attributes_from.rb
|
7
|
+
tasks/has_attributes_from.rake
|
8
|
+
test/factories/general.rb
|
9
|
+
test/rails_root/app/models/customer.rb
|
10
|
+
test/rails_root/app/models/customer_detail.rb
|
11
|
+
test/rails_root/app/models/employee.rb
|
12
|
+
test/rails_root/app/models/employee_property.rb
|
13
|
+
test/rails_root/app/models/manager.rb
|
14
|
+
test/rails_root/app/models/person.rb
|
15
|
+
test/rails_root/config/boot.rb
|
16
|
+
test/rails_root/config/database.yml
|
17
|
+
test/rails_root/config/environment.rb
|
18
|
+
test/rails_root/config/environments/test.rb
|
19
|
+
test/rails_root/config/routes.rb
|
20
|
+
test/rails_root/db/migrate/20090820105348_create_people.rb
|
21
|
+
test/rails_root/db/migrate/20090820105521_create_employee_properties.rb
|
22
|
+
test/rails_root/db/migrate/20090820110621_create_customer_details.rb
|
23
|
+
test/rails_root/db/schema.rb
|
24
|
+
test/rails_root/db/test.sqlite3
|
25
|
+
test/test_helper.rb
|
26
|
+
test/unit/customer_detail_test.rb
|
27
|
+
test/unit/customer_test.rb
|
28
|
+
test/unit/employee_property_test.rb
|
29
|
+
test/unit/employee_test.rb
|
30
|
+
test/unit/manager_test.rb
|
31
|
+
test/unit/person_test.rb
|
32
|
+
with_properties.gemspec
|
data/README.textile
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
h2. Has_attributes_from
|
2
|
+
|
3
|
+
Download: http://github.com/dovadi/has_attributes_from
|
4
|
+
|
5
|
+
h2. Description:
|
6
|
+
|
7
|
+
Merge the attributes from another ActiveRecord class to an individual STI subclass.
|
8
|
+
|
9
|
+
h2. Example:
|
10
|
+
|
11
|
+
<pre>
|
12
|
+
create_table "child_details", :force => true do |t|
|
13
|
+
t.string "nickname"
|
14
|
+
t.string "allergy"
|
15
|
+
t.integer "client_id"
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table "caretaker_details", :force => true do |t|
|
19
|
+
t.string "relation"
|
20
|
+
t.integer "caretaker_id"
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table "people", :force => true do |t|
|
24
|
+
t.string "type"
|
25
|
+
t.string "firstname"
|
26
|
+
t.string "lastname"
|
27
|
+
t.string "email"
|
28
|
+
end
|
29
|
+
|
30
|
+
class Person < ActiveRecord::Base
|
31
|
+
end
|
32
|
+
|
33
|
+
class Child < Person
|
34
|
+
has_attributes_from :child_details
|
35
|
+
validates_presence_of :nickname
|
36
|
+
end
|
37
|
+
|
38
|
+
class Caretaker < Person
|
39
|
+
has_attributes_from :caretaker_details
|
40
|
+
end
|
41
|
+
|
42
|
+
child = Child.create(:firstname =>'William', :lastname=>'Gates' :email=>'bgates@microsoft.com', :nickname=>'Bill')
|
43
|
+
child.update_attributes(:allergy=>'Not allowed to eat apples!')
|
44
|
+
|
45
|
+
caretaker = Caretaker.create(:firstname =>'William H.', :lastname=>'Gates' :email=>'w.h.gates@gmail.com')
|
46
|
+
caretaker.update_attribute(:relation, 'Daddy')
|
47
|
+
</pre>
|
48
|
+
|
49
|
+
h2. Install:
|
50
|
+
|
51
|
+
<pre>
|
52
|
+
* git clone http://github.com/dovadi/has_attributes_from or
|
53
|
+
* sudo gem install dovadi-has_attributes_from -s http://gems.github.com or
|
54
|
+
* Add the following to your environment.rb:
|
55
|
+
|
56
|
+
config.gem 'dovadi-has_attributes_from',
|
57
|
+
:lib => 'has_attributes_from',
|
58
|
+
:version => '>=0.1.1',
|
59
|
+
:source => 'http://gems.github.com'
|
60
|
+
</pre>
|
61
|
+
|
62
|
+
h2. Test
|
63
|
+
|
64
|
+
* You can run the tests from this gem with rake test:default (you need to install the Shoulda and Factory_girl gems).
|
65
|
+
|
66
|
+
h2. Credits
|
67
|
+
|
68
|
+
This project is funded and maintained by "Agile Dovadi BV":http://dovadi.com, contact "Frank Oxener":mailto:frank@dovadi.com
|
69
|
+
|
70
|
+
|
71
|
+
h2. Licence:
|
72
|
+
|
73
|
+
<pre>
|
74
|
+
(The MIT License)
|
75
|
+
|
76
|
+
Copyright (c) 2009 Frank Oxener - Agile Dovadi B.V.
|
77
|
+
|
78
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
79
|
+
a copy of this software and associated documentation files (the
|
80
|
+
'Software'), to deal in the Software without restriction, including
|
81
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
82
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
83
|
+
permit persons to whom the Software is furnished to do so, subject to
|
84
|
+
the following conditions:
|
85
|
+
|
86
|
+
The above copyright notice and this permission notice shall be
|
87
|
+
included in all copies or substantial portions of the Software.
|
88
|
+
|
89
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
90
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
91
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
92
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
93
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
94
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
95
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
96
|
+
</pre>
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module HasAttributesFrom
|
2
|
+
module Base
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def has_attributes_from table
|
11
|
+
@properties = table.to_s
|
12
|
+
has_one table
|
13
|
+
before_save :save_properties
|
14
|
+
|
15
|
+
define_getters_and_setters
|
16
|
+
define_save_properties
|
17
|
+
override_create
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def define_save_properties
|
23
|
+
class_eval %{
|
24
|
+
def save_properties
|
25
|
+
#{@properties}.save if #{@properties} && #{@properties}.changed?
|
26
|
+
end
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def define_getters_and_setters
|
31
|
+
column_names = @properties.camelize.constantize.column_names.reject {|column| column=~/^id$|_id|_at/}
|
32
|
+
column_names.each do |column|
|
33
|
+
class_eval %{
|
34
|
+
def #{column}
|
35
|
+
#{@properties}.nil? ? '' : #{@properties}.#{column}
|
36
|
+
end
|
37
|
+
def #{column}=(value)
|
38
|
+
#{@properties}.#{column}=value if #{@properties}
|
39
|
+
end
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def override_create
|
45
|
+
instance_eval %{
|
46
|
+
def create(attributes = nil, &block)
|
47
|
+
attributes.stringify_keys!
|
48
|
+
attributes_without_associated = attributes.reject{|k,v| k if reflections[k.to_sym]}
|
49
|
+
|
50
|
+
attribute_keys = attributes_without_associated.keys.map{|k| k.to_s}
|
51
|
+
attributes_only_associated_keys = attributes.keys - attributes_without_associated.keys
|
52
|
+
|
53
|
+
property_attribute_keys = (attribute_keys - column_names)
|
54
|
+
parent_attribute_keys = attribute_keys - property_attribute_keys
|
55
|
+
|
56
|
+
parent_attributes = {}
|
57
|
+
property_attributes ={}
|
58
|
+
attributes.each do |k,v|
|
59
|
+
parent_attributes[k]=v if parent_attribute_keys.include?(k) || attributes_only_associated_keys.include?(k)
|
60
|
+
property_attributes[k]=v if property_attribute_keys.include?(k)
|
61
|
+
end
|
62
|
+
instance_of_property_model = "#{@properties}".camelize.constantize.new(property_attributes)
|
63
|
+
|
64
|
+
attributes = parent_attributes.merge(:#{@properties}=>instance_of_property_model)
|
65
|
+
|
66
|
+
super
|
67
|
+
end
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
namespace :test do
|
4
|
+
desc "Run the with_properties tests"
|
5
|
+
Rake::TestTask.new(:has_attributes_from) do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/unit/*_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Run the tests"
|
13
|
+
task :default => ['test:has_attributes_from']
|
14
|
+
|
15
|
+
namespace :has_attributes_from do
|
16
|
+
gem_spec = Gem::Specification.new do |gem_spec|
|
17
|
+
gem_spec.name = "has_attributes_from"
|
18
|
+
gem_spec.version = "0.1.2"
|
19
|
+
gem_spec.summary = "Merge the attributes from another ActiveRecord class to an individual STI subclass."
|
20
|
+
gem_spec.email = "frank.oxener@dovadi.com"
|
21
|
+
gem_spec.homepage = "http://github.com/dovadi/has_attributes_from"
|
22
|
+
gem_spec.description = "Merge the attributes from another ActiveRecord class to an individual STI subclass."
|
23
|
+
gem_spec.authors = ["Frank Oxener"]
|
24
|
+
gem_spec.files = FileList["[A-Z]*", "{lib,tasks,test}/**/*"]
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Generate a gemspec file"
|
28
|
+
task :gemspec do
|
29
|
+
File.open("#{gem_spec.name}.gemspec", 'w') do |f|
|
30
|
+
f.write gem_spec.to_yaml
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
Factory.sequence :email do |n|
|
2
|
+
"person#{n}@example.com"
|
3
|
+
end
|
4
|
+
|
5
|
+
Factory.sequence :name do |n|
|
6
|
+
"name-#{n}"
|
7
|
+
end
|
8
|
+
|
9
|
+
Factory.define :person do |person|
|
10
|
+
person.email {Factory.next :email}
|
11
|
+
person.name {Factory.next :name }
|
12
|
+
end
|
13
|
+
|
14
|
+
Factory.define :customer do |customer|
|
15
|
+
end
|
16
|
+
|
17
|
+
Factory.define :manager do |customer|
|
18
|
+
end
|
19
|
+
|
20
|
+
Factory.define :employee do |customer|
|
21
|
+
end
|
22
|
+
|
23
|
+
Factory.define :customer_detail do |customer_detail|
|
24
|
+
customer_detail.phone ""
|
25
|
+
end
|
26
|
+
|
27
|
+
Factory.define :employee_property do |employee_property|
|
28
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Don't change this file!
|
2
|
+
# Configure your app in config/environment.rb and config/environments/*.rb
|
3
|
+
|
4
|
+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
5
|
+
|
6
|
+
module Rails
|
7
|
+
class << self
|
8
|
+
def boot!
|
9
|
+
unless booted?
|
10
|
+
preinitialize
|
11
|
+
pick_boot.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def booted?
|
16
|
+
defined? Rails::Initializer
|
17
|
+
end
|
18
|
+
|
19
|
+
def pick_boot
|
20
|
+
(vendor_rails? ? VendorBoot : GemBoot).new
|
21
|
+
end
|
22
|
+
|
23
|
+
def vendor_rails?
|
24
|
+
File.exist?("#{RAILS_ROOT}/vendor/rails")
|
25
|
+
end
|
26
|
+
|
27
|
+
def preinitialize
|
28
|
+
load(preinitializer_path) if File.exist?(preinitializer_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def preinitializer_path
|
32
|
+
"#{RAILS_ROOT}/config/preinitializer.rb"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Boot
|
37
|
+
def run
|
38
|
+
load_initializer
|
39
|
+
Rails::Initializer.run(:set_load_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class VendorBoot < Boot
|
44
|
+
def load_initializer
|
45
|
+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
46
|
+
Rails::Initializer.run(:install_gem_spec_stubs)
|
47
|
+
Rails::GemDependency.add_frozen_gem_path
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class GemBoot < Boot
|
52
|
+
def load_initializer
|
53
|
+
self.class.load_rubygems
|
54
|
+
load_rails_gem
|
55
|
+
require 'initializer'
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_rails_gem
|
59
|
+
if version = self.class.gem_version
|
60
|
+
gem 'rails', version
|
61
|
+
else
|
62
|
+
gem 'rails'
|
63
|
+
end
|
64
|
+
rescue Gem::LoadError => load_error
|
65
|
+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
class << self
|
70
|
+
def rubygems_version
|
71
|
+
Gem::RubyGemsVersion rescue nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def gem_version
|
75
|
+
if defined? RAILS_GEM_VERSION
|
76
|
+
RAILS_GEM_VERSION
|
77
|
+
elsif ENV.include?('RAILS_GEM_VERSION')
|
78
|
+
ENV['RAILS_GEM_VERSION']
|
79
|
+
else
|
80
|
+
parse_gem_version(read_environment_rb)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def load_rubygems
|
85
|
+
require 'rubygems'
|
86
|
+
min_version = '1.3.1'
|
87
|
+
unless rubygems_version >= min_version
|
88
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
rescue LoadError
|
93
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
94
|
+
exit 1
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_gem_version(text)
|
98
|
+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
def read_environment_rb
|
103
|
+
File.read("#{RAILS_ROOT}/config/environment.rb")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# All that for this:
|
110
|
+
Rails.boot!
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3-ruby (not necessary on OS X Leopard)
|
3
|
+
|
4
|
+
# Warning: The database defined as "test" will be erased and
|
5
|
+
# re-generated from your development database when you run "rake".
|
6
|
+
# Do not set this db to the same as development or production.
|
7
|
+
test:
|
8
|
+
adapter: sqlite3
|
9
|
+
database: db/test.sqlite3
|
10
|
+
pool: 5
|
11
|
+
timeout: 5000
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file
|
2
|
+
|
3
|
+
# Specifies gem version of Rails to use when vendor/rails is not present
|
4
|
+
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
|
5
|
+
|
6
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
7
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
config.action_controller.session = { :key => "_myapp_session",
|
11
|
+
:secret => "some secret phrase which is longer than 30 characters" }
|
12
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateEmployeeProperties < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :employee_properties do |t|
|
4
|
+
t.integer :reports_to
|
5
|
+
t.string :department
|
6
|
+
t.integer :employee_id
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :employee_properties
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead of editing this file,
|
2
|
+
# please use the migrations feature of Active Record to incrementally modify your database, and
|
3
|
+
# then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
|
6
|
+
# to create the application database on another system, you should be using db:schema:load, not running
|
7
|
+
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
8
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
9
|
+
#
|
10
|
+
# It's strongly recommended to check this file into your version control system.
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(:version => 20090820110621) do
|
13
|
+
|
14
|
+
create_table "customer_details", :force => true do |t|
|
15
|
+
t.string "phone"
|
16
|
+
t.integer "customer_id"
|
17
|
+
t.datetime "created_at"
|
18
|
+
t.datetime "updated_at"
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table "employee_properties", :force => true do |t|
|
22
|
+
t.integer "reports_to"
|
23
|
+
t.string "department"
|
24
|
+
t.integer "employee_id"
|
25
|
+
t.datetime "created_at"
|
26
|
+
t.datetime "updated_at"
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table "people", :force => true do |t|
|
30
|
+
t.string "type"
|
31
|
+
t.string "name"
|
32
|
+
t.string "email"
|
33
|
+
t.datetime "created_at"
|
34
|
+
t.datetime "updated_at"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
Binary file
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Using the STI example from 'Agile Web development with Rails' Second Edition - 18.4
|
2
|
+
|
3
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
$: << File.join(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
ENV["RAILS_ENV"] = "test"
|
7
|
+
|
8
|
+
require 'has_attributes_from'
|
9
|
+
require File.expand_path(File.dirname(__FILE__) + "/rails_root/config/environment")
|
10
|
+
require 'test_help'
|
11
|
+
require 'shoulda'
|
12
|
+
require 'factory_girl'
|
13
|
+
require 'test/factories/general'
|
14
|
+
require 'set'
|
15
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CustomerDetailTest < Test::Unit::TestCase
|
4
|
+
context 'A customer_detail' do
|
5
|
+
setup do
|
6
|
+
@customer_detail = Factory(:customer_detail)
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'be valid' do
|
10
|
+
assert_valid @customer_detail
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CustomerTest < Test::Unit::TestCase
|
4
|
+
context 'A customer with_properties' do
|
5
|
+
setup do
|
6
|
+
@customer = Factory(:customer)
|
7
|
+
@customer.customer_detail=Factory(:customer_detail, :phone=>'0222311304', :customer_id=>@customer)
|
8
|
+
@customer.save
|
9
|
+
end
|
10
|
+
|
11
|
+
should_have_one :customer_detail
|
12
|
+
should_have_class_methods :has_attributes_from
|
13
|
+
should_have_instance_methods :save_properties
|
14
|
+
should_have_instance_methods :phone
|
15
|
+
should_have_instance_methods :phone=
|
16
|
+
|
17
|
+
should 'be valid' do
|
18
|
+
assert_valid @customer
|
19
|
+
end
|
20
|
+
|
21
|
+
should 'return its phone number (based on customer properties)' do
|
22
|
+
assert_equal '0222311304', @customer.phone
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'create a customer with its properties' do
|
26
|
+
customer = Customer.create(:name =>'Dovadi', :email=>'frank@dovadi.com', :phone=>'0649416406')
|
27
|
+
assert_valid customer
|
28
|
+
assert_valid customer.customer_detail
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'update attributes with a property' do
|
32
|
+
customer = Customer.create(:name =>'Dovadi', :email=>'frank@dovadi.com')
|
33
|
+
customer.update_attributes(:phone=>'0222300100')
|
34
|
+
customer.reload
|
35
|
+
assert_equal '0222300100', customer.phone
|
36
|
+
end
|
37
|
+
|
38
|
+
should 'update one attribute with a property' do
|
39
|
+
customer = Customer.create(:name =>'Dovadi', :email=>'frank@dovadi.com')
|
40
|
+
customer.update_attribute(:phone,'0222300200')
|
41
|
+
customer.reload
|
42
|
+
assert_equal '0222300200', customer.phone
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'Validations of a property (through customer_detail)' do
|
47
|
+
setup do
|
48
|
+
@customer = Factory(:customer)
|
49
|
+
@customer.customer_detail=Factory(:customer_detail, :phone=>'0222311304', :customer_id=>@customer)
|
50
|
+
@customer.save
|
51
|
+
end
|
52
|
+
should_ensure_length_in_range :phone, 10..10
|
53
|
+
|
54
|
+
should "validate length of phone (which is a property through customer_detail)" do
|
55
|
+
@customer.phone="012345678"
|
56
|
+
assert !@customer.save
|
57
|
+
end
|
58
|
+
|
59
|
+
should "validate format of phone (which is a property through customer_detail)" do
|
60
|
+
@customer.phone="012345678A"
|
61
|
+
assert !@customer.save
|
62
|
+
end
|
63
|
+
|
64
|
+
should "allow blank a phone number (which is a property through customer_detail)" do
|
65
|
+
@customer.phone=""
|
66
|
+
assert @customer.save
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class EmployeePropertyTest < Test::Unit::TestCase
|
4
|
+
context 'A employee_property' do
|
5
|
+
setup do
|
6
|
+
@employee_property = Factory(:employee_property)
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'be valid' do
|
10
|
+
assert_valid @employee_property
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class EmployeeTest < Test::Unit::TestCase
|
4
|
+
context 'An employee with_properties' do
|
5
|
+
setup do
|
6
|
+
@employee = Factory(:employee)
|
7
|
+
@employee.employee_property=Factory(:employee_property, :reports_to=>123, :department=>'Sales', :employee_id=>@employee)
|
8
|
+
@employee.save
|
9
|
+
end
|
10
|
+
|
11
|
+
should_have_one :employee_property
|
12
|
+
should_have_class_methods :has_attributes_from
|
13
|
+
should_have_instance_methods :save_properties
|
14
|
+
should_have_instance_methods :department
|
15
|
+
should_have_instance_methods :department=
|
16
|
+
should_have_instance_methods :reports_to
|
17
|
+
should_have_instance_methods :reports_to=
|
18
|
+
|
19
|
+
should 'be valid' do
|
20
|
+
assert_valid @employee
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'return its department (based on employee properties)' do
|
24
|
+
assert_equal 'Sales', @employee.department
|
25
|
+
end
|
26
|
+
|
27
|
+
should 'create a employee with its properties' do
|
28
|
+
employee = Employee.create(:name =>'Dovadi', :email=>'frank@dovadi.com', :reports_to=>55, :department=>'Customer support')
|
29
|
+
assert_valid employee
|
30
|
+
assert_valid employee.employee_property
|
31
|
+
end
|
32
|
+
|
33
|
+
should 'update attributes with a property' do
|
34
|
+
employee = Employee.create(:name =>'Dovadi', :email=>'frank@dovadi.com')
|
35
|
+
employee.update_attributes(:reports_to=>100)
|
36
|
+
employee.reload
|
37
|
+
assert_equal 100, employee.reports_to
|
38
|
+
end
|
39
|
+
|
40
|
+
should 'update one attribute with a property' do
|
41
|
+
employee = Employee.create(:name =>'Dovadi', :email=>'frank@dovadi.com')
|
42
|
+
employee.update_attribute(:reports_to,200)
|
43
|
+
employee.reload
|
44
|
+
assert_equal 200, employee.reports_to
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_attributes_from
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frank Oxener
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-21 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Merge the attributes from another ActiveRecord class to an individual STI subclass.
|
17
|
+
email: frank.oxener@dovadi.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- History.txt
|
26
|
+
- Manifest.txt
|
27
|
+
- Rakefile
|
28
|
+
- README.textile
|
29
|
+
- lib/has_attributes_from/base.rb
|
30
|
+
- lib/has_attributes_from.rb
|
31
|
+
- tasks/has_attributes_from.rake
|
32
|
+
- test/factories/general.rb
|
33
|
+
- test/rails_root/app/models/customer.rb
|
34
|
+
- test/rails_root/app/models/customer_detail.rb
|
35
|
+
- test/rails_root/app/models/employee.rb
|
36
|
+
- test/rails_root/app/models/employee_property.rb
|
37
|
+
- test/rails_root/app/models/manager.rb
|
38
|
+
- test/rails_root/app/models/person.rb
|
39
|
+
- test/rails_root/config/boot.rb
|
40
|
+
- test/rails_root/config/database.yml
|
41
|
+
- test/rails_root/config/environment.rb
|
42
|
+
- test/rails_root/config/environments/test.rb
|
43
|
+
- test/rails_root/config/routes.rb
|
44
|
+
- test/rails_root/db/migrate/20090820105348_create_people.rb
|
45
|
+
- test/rails_root/db/migrate/20090820105521_create_employee_properties.rb
|
46
|
+
- test/rails_root/db/migrate/20090820110621_create_customer_details.rb
|
47
|
+
- test/rails_root/db/schema.rb
|
48
|
+
- test/rails_root/db/test.sqlite3
|
49
|
+
- test/test_helper.rb
|
50
|
+
- test/unit/customer_detail_test.rb
|
51
|
+
- test/unit/customer_test.rb
|
52
|
+
- test/unit/employee_property_test.rb
|
53
|
+
- test/unit/employee_test.rb
|
54
|
+
- test/unit/manager_test.rb
|
55
|
+
- test/unit/person_test.rb
|
56
|
+
has_rdoc: false
|
57
|
+
homepage: http://github.com/dovadi/has_attributes_from
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.3.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Merge the attributes from another ActiveRecord class to an individual STI subclass.
|
84
|
+
test_files: []
|
85
|
+
|