Class: Connection
- Inherits:
-
Object
- Object
- Connection
- Defined in:
- lib/unit4/checkout/connection.rb
Overview
creates the connection for both Rails and non-Rails applications
Instance Method Summary collapse
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #non_rails_db_config ⇒ Object
- #rails_db_config ⇒ Object
- #setup_db_config ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 |
# File 'lib/unit4/checkout/connection.rb', line 8 def initialize db_config = setup_db_config ActiveRecord::Base.establish_connection(adapter: db_config["adapter"], database: db_config["database"]) end |
Instance Method Details
#non_rails_db_config ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/unit4/checkout/connection.rb', line 22 def non_rails_db_config # plain Ruby doesn't really have a sense of environments (unless a custom one has been set up) # the development key will most likely be changed when the gem is used by a plain Ruby application # the database db and config used here are samples from a Rails application, but this works with plain Ruby too # Future work: test thoroughly on plain Ruby applications YAML.safe_load(ERB.new(File.read("./config/database.yml")).result, aliases: true)["development"] end |
#rails_db_config ⇒ Object
18 19 20 |
# File 'lib/unit4/checkout/connection.rb', line 18 def rails_db_config Rails.application.config.database_configuration[Rails.env] end |
#setup_db_config ⇒ Object
13 14 15 16 |
# File 'lib/unit4/checkout/connection.rb', line 13 def setup_db_config # decide whether the application that uses the gem is a Rails one or a plain Ruby defined?(Rails) && defined?(Rails.env) ? rails_db_config : non_rails_db_config end |