Class: RubySync::Connectors::ActiveRecordConnector
Overview
You can initialize this connector with the name of a model and the path to a rails application: eg: vault :ActiveRecord, :application=>‘path/to/rails/application’, :model=>:user
Instance Attribute Summary
#is_vault, #name, #once_only
Class Method Summary
collapse
Instance Method Summary
collapse
#associate, #association_context, #association_for, #association_key_for, #association_to_path_dbm_filename, #associations_for, #can_act_as_vault?, class_for, class_name_for, #clean, #create_operations_for, #dbm_path, #digest, #each_change, #entry_for_own_association_key, #find_associated, #has_entry_for_key?, #is_delete_echo?, #is_echo?, #is_vault?, #mirror_dbm_filename, #own_association_key_for, #path_for_association, #path_for_own_association_key, #path_to_association_dbm_filename, #perform_operations, #remove_association, #remove_associations, #remove_mirror, #start, #started, #stop, #stopped, #test_add, #test_delete, #test_modify
Methods included from Utilities
#base_path, #call_if_exists, #connector_called, #ensure_dir_exists, #find_base_path, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #pipeline_called, #set_preference, #something_called, #with_rescue
Constructor Details
Returns a new instance of ActiveRecordConnector.
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 44
def initialize options={}
super options
if application
rails_app_path = File.expand_path(application, File.dirname(__FILE__))
db_config_filename = File.join(rails_app_path, 'config', 'database.yml')
db_config = YAML::load(ERB.new(IO.read(db_config_filename)).result)[rails_env]
log.debug "Loading the models for #{self.class.name}:"
Dir.chdir(File.join(rails_app_path,'app','models')) do
Dir.glob('*.rb') do |filename|
log.debug("\t#{filename}")
require filename
class_name = filename[0..-4].camelize
klass = class_name.constantize
klass.establish_connection db_config if defined? klass.establish_connection
end
end
end
self.class.ar_class model.to_s.camelize.constantize
end
|
Class Method Details
71
72
73
74
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 71
def self.fields
c = self.new
c.ar_class.content_columns.map {|col| col.name }
end
|
.sample_config ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 76
def self.sample_config
return <<END
application '/path/to/a/rails/application'
model 'name_of_model_to_sync'
END
end
|
Instance Method Details
126
127
128
129
130
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 126
def [](path)
entry_from_active_record(ar_class.find(path))
rescue ActiveRecord::RecordNotFound
return nil
end
|
#delete(path) ⇒ Object
122
123
124
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 122
def delete(path)
ar_class.destroy path
end
|
#each_entry ⇒ Object
86
87
88
89
90
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 86
def each_entry
ar_class.find :all do |record|
yield entry_from_active_record(record)
end
end
|
#modify(path, operations) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 115
def modify(path, operations)
ar_class.find(path) do |record|
populate(record, perform_operations(operations))
record.save
end
end
|
Override default perform_add because ActiveRecord is different in that the target path is ignored when adding a record. ActiveRecord determines the id on creation.
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 97
def perform_add event
log.info "Adding '#{event.target_path}' to '#{name}'"
ar_class.new() do |record|
populate(record, perform_operations(event.payload))
log.info(record.inspect)
record.save!
update_mirror record.id
if is_vault?
associate event.association, record.id
end
record.id
end
rescue => ex
log.warn ex
return nil
end
|