antonjenkins-model_sync 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -1
- data/Rakefile +1 -1
- data/lib/model_sync.rb +5 -2
- data/model_sync.gemspec +2 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -6,7 +6,9 @@ model_sync is a simple gem for pushing changes to one of your rails models to an
|
|
6
6
|
model_sync :sync_to => :student,
|
7
7
|
:relationship => { :student_id => :s_stno },
|
8
8
|
:mappings => { :forename => :s_forename },
|
9
|
-
|
9
|
+
:mapping_block => Proc.new do |master, slave|
|
10
|
+
slave.update_attribute(:s_sex, master.gender ? master.gender.code : nil)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
Assuming we have a Student model, the above code will add an after_save callback to the User model which will push changes to the forename value into the s_forename value of Student. The correct Student instance is found using the :relationship hash - student_id in User must equal s_stno in Student.
|
@@ -23,6 +25,16 @@ Once a user books a course and becomes a student as well we've got a user and a
|
|
23
25
|
|
24
26
|
The model_sync gem will do all the syncing for me in a totally transparent way.
|
25
27
|
|
28
|
+
== Installation
|
29
|
+
|
30
|
+
If you've not already added github as a source for gem then do so:
|
31
|
+
|
32
|
+
gem sources -a http://gems.github.com
|
33
|
+
|
34
|
+
Now you can install the gem:
|
35
|
+
|
36
|
+
sudo gem install antonjenkins-model_sync
|
37
|
+
|
26
38
|
== Limitations
|
27
39
|
|
28
40
|
Currently the changes are pushed over with an after_save hook using update_attribute on the :sync_to model. This bypasses validations and any failures on update_attribute are currently not handled.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('model_sync', '0.1.
|
5
|
+
Echoe.new('model_sync', '0.1.2') do |p|
|
6
6
|
p.description = "Sync changes to an ActiveRecord model to another model"
|
7
7
|
p.url = "http://github.com/antonjenkins/model_sync"
|
8
8
|
p.author = "Anton Jenkins"
|
data/lib/model_sync.rb
CHANGED
@@ -24,7 +24,7 @@ module ModelSync
|
|
24
24
|
def sync_changes
|
25
25
|
# If we can find a slave instance...
|
26
26
|
if slave_instance = find_slave_instance
|
27
|
-
# ...
|
27
|
+
# ...then update all the attributes which we've mapped
|
28
28
|
self.class.mappings.each do |source, dest|
|
29
29
|
slave_instance.update_attribute(dest, self.read_attribute(source))
|
30
30
|
end
|
@@ -35,8 +35,11 @@ module ModelSync
|
|
35
35
|
|
36
36
|
private
|
37
37
|
def find_slave_instance
|
38
|
+
# return nil if we don't have a value for the foreign key
|
39
|
+
return nil unless foreign_key_value = self.read_attribute(self.class.relationship.keys.first)
|
40
|
+
# find the instance of the slave class using the relationship hash
|
38
41
|
self.class.slave_model_class.find(:first,
|
39
|
-
:conditions => "#{self.class.relationship.values.first.to_s} = #{
|
42
|
+
:conditions => "#{self.class.relationship.values.first.to_s} = #{foreign_key_value}")
|
40
43
|
end
|
41
44
|
end
|
42
45
|
end
|
data/model_sync.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{model_sync}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Anton Jenkins"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-06}
|
10
10
|
s.description = %q{Sync changes to an ActiveRecord model to another model}
|
11
11
|
s.email = %q{info@pixellatedvisions.com}
|
12
12
|
s.extra_rdoc_files = ["lib/model_sync.rb", "README.rdoc"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: antonjenkins-model_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Jenkins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-06 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|