synchronized_models 1.0.0alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +86 -0
- data/Rakefile +21 -0
- data/lib/generators/synchronized_models_initializer/USAGE +8 -0
- data/lib/generators/synchronized_models_initializer/synchronized_models_initializer_generator.rb +7 -0
- data/lib/generators/synchronized_models_initializer/templates/synchronized_models.rb +21 -0
- data/lib/synchronized_models.rb +18 -0
- data/lib/synchronized_models/configuration.rb +14 -0
- data/lib/synchronized_models/kernel_synchronizer.rb +84 -0
- data/lib/synchronized_models/railtie.rb +4 -0
- data/lib/synchronized_models/version.rb +3 -0
- data/lib/tasks/synchronized_models_tasks.rake +4 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '06807cd51b2bde960cc3509e04cb60bd60ef30b1befcb2a64d4a876822e91f9c'
|
4
|
+
data.tar.gz: ba4fd66b6ccb81e868d124ccd87600af1f69144097106be68fdab23b9accc523
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 072032b19d695ed7fb95506ea8f48be0cab4e5d320672a35f047fcfe3876d28f913ad0de1ecec22667b14c5d046d1266ac6c0b50650fe5edc43afc4f368106da
|
7
|
+
data.tar.gz: 19500b7a4ba1d2ea44bb260896476dfa142a23925910096b745d30bcc4bb053219be37218854c32c008cf736340badd620d4b4692893e3f23aca64c413e53a36
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# SynchronizedModels
|
2
|
+
|
3
|
+
Add a model into another model with an external database connection.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/armando1339/synchronized_models.svg?branch=master)](https://travis-ci.org/armando1339/synchronized_models) [![Coverage Status](https://coveralls.io/repos/github/armando1339/synchronized_models/badge.svg?branch=master)](https://coveralls.io/github/armando1339/synchronized_models?branch=master)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'synchronized_models'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
In the root directory:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
$ rails generate synchronized_models_initializer
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Set the values of the external database in the configuration file that was created in config/initializers/synchronized_models.rb by `rails generate synchronized_models_initializer` command.
|
30
|
+
|
31
|
+
Example:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Account < ActiveRecord::Base
|
35
|
+
sync_remote_model model_name: :account
|
36
|
+
end
|
37
|
+
|
38
|
+
# Then:
|
39
|
+
|
40
|
+
Account::RemoteAccount.last
|
41
|
+
Account::RemoteAccount.create name: "Some name"
|
42
|
+
```
|
43
|
+
|
44
|
+
Or:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Account < ActiveRecord::Base
|
48
|
+
sync_remote_model model_name: :user
|
49
|
+
end
|
50
|
+
|
51
|
+
# Then:
|
52
|
+
|
53
|
+
Account::RemoteUser.last
|
54
|
+
Account::RemoteUser.create email: user@example.com, password: 123456
|
55
|
+
```
|
56
|
+
|
57
|
+
Or:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class Account < ActiveRecord::Base
|
61
|
+
sync_remote_model model_name: :user, table_name: :admins
|
62
|
+
end
|
63
|
+
|
64
|
+
# Then:
|
65
|
+
|
66
|
+
Account::RemoteUser.last
|
67
|
+
Account::RemoteUser.create email: user@example.com, password: 123456
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug report or pull request are welcome.
|
73
|
+
|
74
|
+
Make a pull request:
|
75
|
+
|
76
|
+
- Clone the repo
|
77
|
+
- Create your feature branch
|
78
|
+
- Commit your changes
|
79
|
+
- Push the branch
|
80
|
+
- Create new Pull-Request
|
81
|
+
|
82
|
+
Please write tests if necessary.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SynchronizedModels'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
require "rspec/core/rake_task"
|
19
|
+
|
20
|
+
RSpec::Core::RakeTask.new(:spec)
|
21
|
+
task default: :spec
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# => This initializer contains the references
|
2
|
+
# to the external database. The assigned values
|
3
|
+
# depend on the environment that is running
|
4
|
+
# (develpment, staging or production).
|
5
|
+
#
|
6
|
+
SynchronizedModels.configure do |config|
|
7
|
+
# => optional parameter that
|
8
|
+
# contain the values 'utf8'
|
9
|
+
# by default
|
10
|
+
#
|
11
|
+
# config.encoding = 'utf8'
|
12
|
+
#
|
13
|
+
# => Required parameters without
|
14
|
+
# default values.
|
15
|
+
#
|
16
|
+
config.adapter = ENV['EXTERNAL_DB_ADAPTER']
|
17
|
+
config.host = ENV['EXTERNAL_DB_HOST']
|
18
|
+
config.username = ENV['EXTERNAL_DB_USERNAME']
|
19
|
+
config.password = ENV['EXTERNAL_DB_PASSWORD']
|
20
|
+
config.database = ENV['EXTERNAL_DB_NAME']
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'synchronized_models/kernel_synchronizer'
|
2
|
+
require 'synchronized_models/configuration'
|
3
|
+
|
4
|
+
module SynchronizedModels
|
5
|
+
attr_accessor :configuration
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SynchronizedModels
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :adapter, :encoding, :host, :username, :password, :database
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@adapter = 'postgresql'
|
7
|
+
@encoding = 'utf8'
|
8
|
+
@host = nil
|
9
|
+
@username = nil
|
10
|
+
@password = nil
|
11
|
+
@database = nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Kernel
|
2
|
+
#
|
3
|
+
# => This method is executed as a callback to generate an
|
4
|
+
# ActiveRecord::Base constant, within the scope of the same
|
5
|
+
# model where it is called, which will be connected to a
|
6
|
+
# second database.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# class Account < ActiveRecord::Base
|
11
|
+
# sync_remote_model model_name: :account
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Then:
|
15
|
+
#
|
16
|
+
# Account::RemoteAccount.last
|
17
|
+
# Account::RemoteAccount.create name: "Some name"
|
18
|
+
#
|
19
|
+
# Or:
|
20
|
+
#
|
21
|
+
# class Account < ActiveRecord::Base
|
22
|
+
# sync_remote_model model_name: :user
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# Then:
|
26
|
+
#
|
27
|
+
# Account::RemoteUser.last
|
28
|
+
# Account::RemoteUser.create email: user@example.com, password: 123456
|
29
|
+
#
|
30
|
+
# Or:
|
31
|
+
#
|
32
|
+
# class Account < ActiveRecord::Base
|
33
|
+
# sync_remote_model model_name: :user, table_name: :admins
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# Then:
|
37
|
+
#
|
38
|
+
# Account::RemoteUser.last
|
39
|
+
# Account::RemoteUser.create email: user@example.com, password: 123456
|
40
|
+
#
|
41
|
+
def sync_remote_model(*args)
|
42
|
+
configure_sync_constant(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# => Create an ActiveRecord::Base constant and add
|
48
|
+
# the remote database configuration that was configured
|
49
|
+
# in config/initializers/synchronized_models.rb
|
50
|
+
#
|
51
|
+
def configure_sync_constant(args={})
|
52
|
+
return false unless ancestors.include? ActiveRecord::Base
|
53
|
+
|
54
|
+
initialize_sync_constant args.fetch(:model_name) do |conts|
|
55
|
+
conts.send :table_name=, select_table_name(args).to_s.tableize
|
56
|
+
conts.establish_connection database_adapter
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# => *
|
61
|
+
def initialize_sync_constant(model_name)
|
62
|
+
const_set(
|
63
|
+
"Remote#{model_name.to_s.capitalize}",
|
64
|
+
Class.new(ActiveRecord::Base)
|
65
|
+
)
|
66
|
+
|
67
|
+
yield synchronized_constant(model_name)
|
68
|
+
end
|
69
|
+
|
70
|
+
# => *
|
71
|
+
def synchronized_constant(model_name)
|
72
|
+
"#{name}::Remote#{model_name.to_s.capitalize}".constantize
|
73
|
+
end
|
74
|
+
|
75
|
+
# => *
|
76
|
+
def select_table_name(args)
|
77
|
+
args.include?(:table_name) ? args.fetch(:table_name) : args.fetch(:model_name)
|
78
|
+
end
|
79
|
+
|
80
|
+
# => *
|
81
|
+
def database_adapter
|
82
|
+
SynchronizedModels.configuration.instance_values
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: synchronized_models
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Armando Alejandre
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mysql2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Add a model into another model with an external database connection.
|
98
|
+
email:
|
99
|
+
- armando1339@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/generators/synchronized_models_initializer/USAGE
|
108
|
+
- lib/generators/synchronized_models_initializer/synchronized_models_initializer_generator.rb
|
109
|
+
- lib/generators/synchronized_models_initializer/templates/synchronized_models.rb
|
110
|
+
- lib/synchronized_models.rb
|
111
|
+
- lib/synchronized_models/configuration.rb
|
112
|
+
- lib/synchronized_models/kernel_synchronizer.rb
|
113
|
+
- lib/synchronized_models/railtie.rb
|
114
|
+
- lib/synchronized_models/version.rb
|
115
|
+
- lib/tasks/synchronized_models_tasks.rake
|
116
|
+
homepage: https://github.com/armando1339/synchronized_models
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata:
|
120
|
+
source_code_uri: https://github.com/armando1339/synchronized_models
|
121
|
+
allowed_push_host: https://rubygems.org
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">"
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: 1.3.1
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.7.8
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Add a model into another model with an external database connection.
|
142
|
+
test_files: []
|