sinatra-activerecord 2.0.18 → 2.0.23

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: fc810f00cad9c2ea25e192e3561b105d86b59470b74f1ed87989956516582a07
4
- data.tar.gz: 6ed1bf13fb1bf569ecd6f13c2e30e733f46f479b78b09202dba084498c7f0788
3
+ metadata.gz: c1985ef88a83eccdcf4ad4db78ced061a9398fd54cd01a0444816f4b83c661da
4
+ data.tar.gz: e449fdc9807e75b4d9994545d08107473b618712b870b37edfb5d84de317aef3
5
5
  SHA512:
6
- metadata.gz: '090c13e2217b2ca868a6224422211a4c22ae95f7a6d2b36819714e0f866153cc05f05b9b6d557a99e3aa1ab69a4c3f62dc57022d9c1c0439c3d07d0cb6474a9b'
7
- data.tar.gz: dc906d8a9fcbc2118f743aaba8c1a19ac0816ed7b5353d0a21fe7308a4e500fbe09ef328d640555600ce5f93ffcb4a2ebfd9890d9e983a520c9c52596a117138
6
+ metadata.gz: b6e0d9905eb59b73e06cb57d9e69f1351c34ffa17a386bac9bc4a520fd9950d28cb5c58d68e09dcb261a9550ed8a58194f72257f466e793c540f664fc0edc945
7
+ data.tar.gz: 60ff2c30ffb5b65b6cb6f9d4a9b9d0aaf39af210ff7c3fcea6a04365b9573b67864d3d4b9331e6193986b499ba6c06ea4d512173114a183442f8a6d30d0e9bea
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,34 @@ 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
+ # if the configuration concerns only one database, and url_spec exist, url_spec will override the same key
35
+ # if the configuration has multiple databases (Rails 6.0+ feature), url_spec is discarded
36
+ # Following Active Record config convention
37
+ # https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169
38
+ final_spec = file_spec.keys.map do |env|
39
+ config = file_spec[env]
40
+ if config.is_a?(Hash) && config.all? { |_k, v| v.is_a?(Hash) }
41
+ [env, config]
42
+ else
43
+ [env, config.merge(url_spec)]
44
+ end
45
+ end.to_h
46
+
47
+ app.set :database, final_spec
48
+ elsif ENV['DATABASE_URL']
20
49
  app.set :database, ENV['DATABASE_URL']
21
50
  elsif File.exist?("#{Dir.pwd}/config/database.yml")
22
51
  app.set :database_file, "#{Dir.pwd}/config/database.yml"
@@ -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.18
4
+ version: 2.0.23
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-16 00:00:00.000000000 Z
13
+ date: 2021-05-12 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.