Class: RubySync::Connectors::ActiveRecordConnector

Inherits:
BaseConnector show all
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

Methods inherited from BaseConnector

#add, #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, #remove_association, #remove_associations, #remove_mirror, #start, #started, #stop, #stopped, #sync_started, #sync_stopped, #test_add, #test_delete, #test_modify

Methods included from Utilities

#as_array, #call_if_exists, #connector_called, #effective_operations, #ensure_dir_exists, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #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 options={}
  super options

  # Rails app specified, use it to configure
  if application
    # Load the database configuration
    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]
    # 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

.fieldsObject



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_configObject



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_entryObject



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