sinatra-activerecord 2.0.16 → 2.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88ba7fc804c9d45c3761d20076e29736c09a7c052cdfbb511307b237e5219036
4
- data.tar.gz: 3efec8a225c7fe78778e1765f4d7c2a122553c86e1de6dd2440e6a5984e10276
3
+ metadata.gz: 9b662752481c4940600cb421378b4dd447e549ea11a876c86a36d250f473e706
4
+ data.tar.gz: ac0a9176f4e2b0abd115664db78ba1bfeb7ed043dd190c3ed5251a794555a360
5
5
  SHA512:
6
- metadata.gz: f568c2dbf32e4b654ac95f1c045fe7db6ffd841e14377d29f12e3f461da857621e8417e1be7c6b88cefcfc78c1a04c463bceaeb0cccb4931b31ce048ef9878f9
7
- data.tar.gz: dd0016fe5c627cbfdf9fddce90b387d9be9738731ee197d8e05e6ad8514faf78f2b61911e3c18d25564389cff89915a400a3a4fb9ea0bf129d7bc24d263f3dd6
6
+ metadata.gz: 6b6bb226f78cc9fd45e928625b3c062077101fb0e8a9f58ad47fd8f374ab316e96cee7366872346c02bfb411306e997a8a2848b433b8103abba9643f2f8f2f48
7
+ data.tar.gz: 7c2b8cd35e0dc75e9ac1045c8a1074cdaa75eedadc6ce607fd8e99a30ba6f3b3e0abae2f0bfeab8276a38c840da529dc9f1cb5d689108c191cd29a36332958ce
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)
@@ -16,7 +16,19 @@ module Sinatra
16
16
 
17
17
  module ActiveRecordExtension
18
18
  def self.registered(app)
19
- if ENV['DATABASE_URL']
19
+ if ENV['DATABASE_URL'] && File.exist?("#{Dir.pwd}/config/database.yml")
20
+ path = "#{Dir.pwd}/config/database.yml"
21
+ url = ENV['DATABASE_URL']
22
+ file_path = File.join(root, path) if Pathname(path).relative? and root
23
+ file_spec = YAML.load(ERB.new(File.read(path)).result) || {}
24
+
25
+ url_spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
26
+
27
+ # url_spec will override the same key, if exist
28
+ final_spec = file_spec.keys.map{ |env| [env, file_spec[env].merge(url_spec)] }.to_h
29
+
30
+ app.set :database, final_spec
31
+ elsif ENV['DATABASE_URL']
20
32
  app.set :database, ENV['DATABASE_URL']
21
33
  elsif File.exist?("#{Dir.pwd}/config/database.yml")
22
34
  app.set :database_file, "#{Dir.pwd}/config/database.yml"
@@ -43,13 +55,21 @@ module Sinatra
43
55
  if spec.is_a?(Hash) and spec.symbolize_keys[environment.to_sym]
44
56
  ActiveRecord::Base.configurations = spec.stringify_keys
45
57
  ActiveRecord::Base.establish_connection(environment.to_sym)
46
- elsif spec.is_a?(Hash)
47
- ActiveRecord::Base.configurations[environment.to_s] = spec.stringify_keys
58
+ elsif spec.is_a?(Hash)
59
+ ActiveRecord::Base.configurations = {
60
+ environment.to_s => spec.stringify_keys
61
+ }
62
+
48
63
  ActiveRecord::Base.establish_connection(spec.stringify_keys)
49
64
  else
65
+ if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.0')
66
+ ActiveRecord::Base.configurations ||= ActiveRecord::DatabaseConfigurations.new({}).resolve(spec)
67
+ else
68
+ ActiveRecord::Base.configurations ||= {}
69
+ ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash
70
+ end
71
+
50
72
  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
73
  end
54
74
  end
55
75
 
@@ -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"
@@ -5,6 +5,14 @@ require "fileutils"
5
5
  namespace :db do
6
6
  desc "Create a migration (parameters: NAME, VERSION)"
7
7
  task :create_migration do
8
+ ARGV.each do |a|
9
+ # when we run 'rake db:create_migration create_users v1',
10
+ # rake will also run 'rake create_users' and 'rake v1'
11
+ # to avoid rake abort, we define an empty method for these (ie: "task :create_users do ; end")
12
+ next if a.nil?
13
+ task a.to_sym do ; end
14
+ end
15
+
8
16
  unless ENV["NAME"] || ARGV[1]
9
17
  puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
10
18
  exit
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.16
4
+ version: 2.0.21
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: 2020-10-24 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.