Class: RubySync::Connectors::ActiveRecordConnector
- Inherits:
-
BaseConnector
- Object
- BaseConnector
- RubySync::Connectors::ActiveRecordConnector
- Defined in:
- lib/ruby_sync/connectors/active_record_connector.rb
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
Attributes inherited from BaseConnector
#is_vault, #name, #once_only, #pipeline
Class Method Summary collapse
Instance Method Summary collapse
- #[](path) ⇒ Object
- #delete(path) ⇒ Object
- #each_entry ⇒ Object
-
#initialize(options = {}) ⇒ ActiveRecordConnector
constructor
A new instance of ActiveRecordConnector.
- #modify(path, operations) ⇒ Object
-
#perform_add(event) ⇒ Object
Override default perform_add because ActiveRecord is different in that the target path is ignored when adding a record.
Methods inherited from BaseConnector
#add, #association_context, #association_for, #can_act_as_vault?, #clean, #create_operations_for, #entry_for_own_association_key, event_method, #find_associated, #has_entry_for_key?, #is_delete_echo?, #is_echo?, #is_vault?, #own_association_key_for, #path_for_own_association_key, #start, #started, #stop, #stopped, #sync_started, #sync_stopped, target_transform, #test_add, #test_delete, #test_modify, track_associations_with, track_changes_with
Methods included from ConnectorEventProcessing
#associated_path, #clean, #delete_from_mirror, #perform_delete, #perform_modify, #process, #update_mirror
Methods included from Utilities
#as_array, #call_if_exists, #class_called, #class_for_name, #class_name_for, #connector_called, #dump_after, #dump_before, #effective_operations, #ensure_dir_exists, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #perform_transform, #pipeline_called, #set_preference, #something_called, #with_rescue
Constructor Details
#initialize(options = {}) ⇒ ActiveRecordConnector
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 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 44 def initialize ={} super # Rails app specified, use it to configure if application # Load the database configuration rails_app_path = File.(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] # Require the models 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
.fields ⇒ Object
70 71 72 73 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 70 def self.fields c = self.new c.ar_class.content_columns.map {|col| col.name } end |
.sample_config ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 75 def self.sample_config return <<END # Uncomment and adjust the following if your app is a Ruby on Rails # application. It will grab the config from the RoR database.yml. # application '/path/to/a/rails/application' # model 'name_of_model_to_sync' # rails_env 'development' # typically development or production # OR # Uncomment and adjust the following if your app is not # a Ruby on Rails application (EXPERIMENTAL) # # db_type 'mysql' # eg 'db2', 'mysql', 'oci', 'postgresql', 'sqlite', 'sqlserver' # db_host 'localhost' # network name of db server # db_name 'database_name' # Name of the database (not the table) # model 'my_model' # class MyModel < ActiveRecord::Base # set_table_name "users" # end END end |
Instance Method Details
#[](path) ⇒ Object
141 142 143 144 145 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 141 def [](path) entry_from_active_record(ar_class.find(path)) rescue ActiveRecord::RecordNotFound return nil end |
#delete(path) ⇒ Object
137 138 139 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 137 def delete(path) ar_class.destroy path end |
#each_entry ⇒ Object
101 102 103 104 105 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 101 def each_entry ar_class.find :all do |record| yield entry_from_active_record(record) end end |
#modify(path, operations) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 130 def modify(path, operations) ar_class.find(path) do |record| populate(record, perform_operations(operations)) record.save end end |
#perform_add(event) ⇒ Object
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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruby_sync/connectors/active_record_connector.rb', line 112 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 |