sinatra-activerecord 2.0.17 → 2.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a97567854628642b73c97356482c4339283aed960de12973456174ae0647fd11
4
- data.tar.gz: cdedb76e50ecc962431c6e51a8fdda4dad0cfe352f03b3da803f334db7a72875
3
+ metadata.gz: dd8c9b0af0329e3c58559409592d58ff44a7110407969effa9902c34c316eb07
4
+ data.tar.gz: fb39eecde9d4947aac413beda568cb3b60ad408d8505fddf45907cbed18193f2
5
5
  SHA512:
6
- metadata.gz: 95afe1d57ab23c9f83667cc8a173aadb86e7751019a1bd67327b960e8c1ea829dd66617addd29caec86ff1e66667e1c78a94c5455a31241d245e8e33776887a8
7
- data.tar.gz: 6616b14f3ae6a53c10bca15b25f749d7bf3913070ce7e31db916b773f32d60b1014716e12b34f3dddc530a52c0904c52582c5c28e52c10712418d3bd71a140f7
6
+ metadata.gz: 8655fe10cab66ccb6d3c4ab505d81e7ae2d091057dcda13b2f8e0504f738274274a4ae4d53c5d6c91bf64f0c5b2be2603467bc62272d2cf7125e124c825b7c37
7
+ data.tar.gz: b04451e063eb7729b3e33937697c13f0d1dd329bfcd7970a8e2d0eda4f7c64fda3d72dc4aa896e16569db2d95a4f0ee158b7068147dac8d8ec53c545296c50d3
data/README.md CHANGED
@@ -35,6 +35,8 @@ If you have a `config/database.yml`, it will automatically be loaded, no need
35
35
  to specify it. Also, in production, the `$DATABASE_URL` environment variable
36
36
  will automatically be read as the database (if you haven't specified otherwise).
37
37
 
38
+ If both `config/database.yml` and `$DATABASE_URL` are present, the database configuration of this two will be merged, with $DATABASE_URL's variable taking precedence over database.yml (for the same variable / key).
39
+
38
40
  Note that in **modular** Sinatra applications you will need to first register
39
41
  the extension:
40
42
 
@@ -147,8 +149,9 @@ This gem was made in 2009 by Blake Mizerany, creator of Sinatra.
147
149
 
148
150
  ## Social
149
151
 
150
- You can follow me on Twitter, I'm [@jankomarohnic](http://twitter.com/jankomarohnic).
152
+ You can follow Janko on Twitter, [@jankomarohnic](http://twitter.com/jankomarohnic).
153
+ You can follow Axel on Twitter, [@soulchildpls](http://twitter.com/soulchildpls).
151
154
 
152
155
  ## License
153
156
 
154
- [MIT](https://github.com/janko-m/sinatra-activerecord/blob/master/LICENSE)
157
+ [MIT](https://github.com/sinatra-activerecord/sinatra-activerecord/blob/master/LICENSE)
@@ -7,6 +7,8 @@ require 'pathname'
7
7
  require 'yaml'
8
8
  require 'erb'
9
9
 
10
+ require 'active_record/database_configurations/connection_url_resolver' if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.1')
11
+
10
12
  module Sinatra
11
13
  module ActiveRecordHelper
12
14
  def database
@@ -16,7 +18,24 @@ module Sinatra
16
18
 
17
19
  module ActiveRecordExtension
18
20
  def self.registered(app)
19
- if ENV['DATABASE_URL']
21
+ if ENV['DATABASE_URL'] && File.exist?("#{Dir.pwd}/config/database.yml")
22
+ path = "#{Dir.pwd}/config/database.yml"
23
+ url = ENV['DATABASE_URL']
24
+ file_path = File.join(root, path) if Pathname(path).relative? and root
25
+ file_spec = YAML.load(ERB.new(File.read(path)).result) || {}
26
+
27
+ # ActiveRecord 6.1+ has moved the connection url resolver to another module
28
+ if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.1')
29
+ url_spec = ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver.new(url).to_hash
30
+ else
31
+ url_spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
32
+ end
33
+
34
+ # url_spec will override the same key, if exist
35
+ final_spec = file_spec.keys.map{ |env| [env, file_spec[env].merge(url_spec)] }.to_h
36
+
37
+ app.set :database, final_spec
38
+ elsif ENV['DATABASE_URL']
20
39
  app.set :database, ENV['DATABASE_URL']
21
40
  elsif File.exist?("#{Dir.pwd}/config/database.yml")
22
41
  app.set :database_file, "#{Dir.pwd}/config/database.yml"
@@ -43,13 +62,21 @@ module Sinatra
43
62
  if spec.is_a?(Hash) and spec.symbolize_keys[environment.to_sym]
44
63
  ActiveRecord::Base.configurations = spec.stringify_keys
45
64
  ActiveRecord::Base.establish_connection(environment.to_sym)
46
- elsif spec.is_a?(Hash)
47
- ActiveRecord::Base.configurations[environment.to_s] = spec.stringify_keys
65
+ elsif spec.is_a?(Hash)
66
+ ActiveRecord::Base.configurations = {
67
+ environment.to_s => spec.stringify_keys
68
+ }
69
+
48
70
  ActiveRecord::Base.establish_connection(spec.stringify_keys)
49
71
  else
72
+ if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.0')
73
+ ActiveRecord::Base.configurations ||= ActiveRecord::DatabaseConfigurations.new({}).resolve(spec)
74
+ else
75
+ ActiveRecord::Base.configurations ||= {}
76
+ ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash
77
+ end
78
+
50
79
  ActiveRecord::Base.establish_connection(spec)
51
- ActiveRecord::Base.configurations ||= {}
52
- ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash
53
80
  end
54
81
  end
55
82
 
@@ -8,7 +8,7 @@ module Rails
8
8
  end
9
9
 
10
10
  def env
11
- ActiveSupport::StringInquirer.new(ENV["RACK_ENV"] || "development")
11
+ ActiveSupport::StringInquirer.new(ENV["APP_ENV"] || ENV["RACK_ENV"] || "development")
12
12
  end
13
13
 
14
14
  def application
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  ActiveRecord::Tasks::DatabaseTasks.tap do |config|
8
8
  config.root = Rake.application.original_dir
9
- config.env = ENV["RACK_ENV"] || "development"
9
+ config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
10
10
  config.db_dir = "db"
11
11
  config.migrations_paths = ["db/migrate"]
12
12
  config.fixtures_path = "test/fixtures"
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  ActiveRecord::Tasks::DatabaseTasks.tap do |config|
8
8
  config.root = Rake.application.original_dir
9
- config.env = ENV["RACK_ENV"] || "development"
9
+ config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
10
10
  config.db_dir = "db"
11
11
  config.migrations_paths = ["db/migrate"]
12
12
  config.fixtures_path = "test/fixtures"
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  ActiveRecord::Tasks::DatabaseTasks.tap do |config|
8
8
  config.root = Rake.application.original_dir
9
- config.env = ENV["RACK_ENV"] || "development"
9
+ config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
10
10
  config.db_dir = "db"
11
11
  config.migrations_paths = ["db/migrate"]
12
12
  config.fixtures_path = "test/fixtures"
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.17
4
+ version: 2.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
8
8
  - Janko Marohnić
9
+ - Axel Kee
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2020-04-15 00:00:00.000000000 Z
13
+ date: 2021-01-18 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: sinatra
@@ -96,7 +97,7 @@ dependencies:
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  description: Extends Sinatra with ActiveRecord helpers.
99
- email: janko.marohnic@gmail.com
100
+ email: axel@rubyyagi.com
100
101
  executables: []
101
102
  extensions: []
102
103
  extra_rdoc_files: []
@@ -110,7 +111,7 @@ files:
110
111
  - lib/sinatra/activerecord/rake/activerecord_5.rb
111
112
  - lib/sinatra/activerecord/rake/activerecord_6.rb
112
113
  - lib/sinatra/activerecord/tasks.rake
113
- homepage: http://github.com/janko-m/sinatra-activerecord
114
+ homepage: http://github.com/sinatra-activerecord/sinatra-activerecord
114
115
  licenses:
115
116
  - MIT
116
117
  metadata: {}
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  - !ruby/object:Gem::Version
130
131
  version: '0'
131
132
  requirements: []
132
- rubygems_version: 3.0.3
133
+ rubygems_version: 3.1.2
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: Extends Sinatra with ActiveRecord helpers.