-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsynchronisable.rb
75 lines (62 loc) · 2.15 KB
/
synchronisable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'active_record'
require 'active_support/core_ext/hash'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/object/deep_dup'
require 'active_support/core_ext/string/inflections'
require 'active_support/configurable'
require 'active_support/concern'
require 'synchronisable/bootstrap/i18n'
require 'synchronisable/version'
require 'synchronisable/configuration'
require 'synchronisable/models/import'
require 'synchronisable/synchronizer'
require 'synchronisable/model'
require 'synchronisable/gateway'
module Synchronisable
def self.config
@configuration ||= Configuration.new
end
def self.configure
yield config
end
# Syncs models that are defined in {Synchronisable#models}
#
# @overload sync(models, options)
# @param models [Array] array of models that should be synchronized.
# This take a precedence over models defined in {Synchronisable#models}.
# If this parameter is not specified and {Synchronisable#models} is empty,
# than it will try to sync only those models which have a corresponding synchronizers
# @param options [Hash] options that will be passed to controller
# @overload sync(models)
# @overlaod sync(options)
#
# @return [Array<[Synchronisable::Context]>] array of synchronization contexts
#
# @see Synchronisable::Context
def self.sync(*args)
options = args.extract_options!
source = source_models(args)
source.map { |model| model.sync(options) }
end
private
def self.source_models(models)
source = models.present? ? models : default_models
source = source.present? ? source : find_models
source.sort { |lhs, rhs| lhs.synchronizer.order <=> rhs.synchronizer.order }
end
def self.default_models
config.models.map(&:safe_constantize).compact
end
def self.find_models
# Need to preload models first
Rails.application.eager_load!
ActiveRecord::Base.descendants.select do |model|
model.included_modules.include?(Synchronisable::Model) &&
model.synchronisable?
end
end
end
ActiveSupport.on_load(:active_record) do
include Synchronisable::Model
end