├── .codeclimate.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── Guardfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── dry-initializer-rails.gemspec ├── lib ├── dry-initializer-rails.rb └── dry │ └── initializer │ └── rails.rb └── spec ├── dry └── initializer │ ├── assignment_by_custom_key_spec.rb │ ├── assignment_by_id_spec.rb │ ├── assignment_by_model_spec.rb │ ├── assignment_by_nil_spec.rb │ ├── base_syntax_spec.rb │ └── modelling_relation_spec.rb ├── dummy ├── Rakefile ├── app │ └── models │ │ ├── item.rb │ │ └── user.rb ├── config │ ├── database.yml │ └── environment.rb ├── db │ ├── migrate │ │ ├── 20160509134800_create_users.rb │ │ └── 20160509134900_create_items.rb │ └── schema.rb └── lib │ ├── dummy.rb │ └── dummy │ └── application.rb └── spec_helper.rb /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | --- 2 | engines: 3 | rubocop: 4 | enabled: true 5 | duplication: 6 | enabled: true 7 | config: 8 | languages: 9 | - ruby 10 | exclude_paths: 11 | - "spec/**/*" 12 | ratings: 13 | paths: 14 | - "lib/**/*" 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | /spec/dummy/db/*.sqlite3 11 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | --- 2 | AllCops: 3 | DisplayCopNames: true 4 | DisplayStyleGuide: true 5 | StyleGuideCopsOnly: true 6 | TargetRubyVersion: 2.3 7 | 8 | Layout/EmptyLinesAroundBlockBody: 9 | Enabled: false 10 | 11 | Metrics/LineLength: 12 | Exclude: 13 | - spec/dummy/db/schema.rb 14 | 15 | Naming/FileName: 16 | Exclude: 17 | - 'lib/dry-initializer-rails.rb' 18 | 19 | Style/CaseEquality: 20 | Enabled: false 21 | 22 | Style/ClassAndModuleChildren: 23 | Enabled: false 24 | 25 | Style/ClassVars: 26 | Enabled: false 27 | 28 | Style/DoubleNegation: 29 | Enabled: false 30 | 31 | Style/Lambda: 32 | Exclude: 33 | - spec/**/*_spec.rb 34 | 35 | Style/LambdaCall: 36 | Enabled: false 37 | 38 | Style/NumericLiterals: 39 | Exclude: 40 | - spec/dummy/db/schema.rb 41 | 42 | Style/RaiseArgs: 43 | EnforcedStyle: compact 44 | 45 | Style/Semicolon: 46 | Exclude: 47 | - spec/**/*_spec.rb 48 | 49 | Style/SignalException: 50 | EnforcedStyle: semantic 51 | 52 | Style/StringLiterals: 53 | EnforcedStyle: double_quotes 54 | 55 | Style/StringLiteralsInInterpolation: 56 | EnforcedStyle: double_quotes 57 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: ruby 3 | cache: bundler 4 | bundler_args: --without benchmarks tools 5 | script: 6 | - bundle exec rake spec 7 | - bundle exec rubocop 8 | rvm: 9 | - 2.3.0 10 | - 2.6.2 11 | - ruby-head 12 | matrix: 13 | allow_failures: 14 | - rvm: ruby-head 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/). 7 | 8 | ## [3.1.1] [2018-05-06] 9 | - support `dry-initializer` v3+ (nepalez) 10 | 11 | ## [3.1.0] [2018-02-12] 12 | - support `ActiveRecord::Relation` in models (nepalez) 13 | 14 | ```ruby 15 | param :user, model: User.where(active: true) 16 | ``` 17 | 18 | ## [3.0.0] [2018-02-01] 19 | 20 | ### Changed 21 | - take `nil` as is without trying to find a record (nepalez) 22 | - raise `ActiveRecord::RecordNotFound` in case of absent key (nepalez) 23 | 24 | ## [2.0.0] [2018-02-01] 25 | 26 | Update dependency to [dry-initializer] v2.4+ (nepalez) 27 | 28 | ## [1.0.0] [2017-01-28] 29 | 30 | Update dependency to [dry-initializer] v1.1+ (nepalez) 31 | 32 | ## [0.0.3] [2016-07-17] 33 | 34 | Update dependency to [dry-initializer] v0.4+ (nepalez) 35 | 36 | ## [0.0.2] [2016-05-19] 37 | 38 | Update dependency to [dry-initializer] v0.3+ (nepalez) 39 | 40 | ## [0.0.1] [2016-05-16] 41 | 42 | First public release 43 | 44 | [dry-initializer]: https://github.com/dry-rb/dry-initializer 45 | [0.0.1]: https://github.com/nepalez/dry-initializer-rails/compare/ab07725...v0.0.1 46 | [0.0.2]: https://github.com/nepalez/dry-initializer-rails/compare/v0.0.1...v0.0.2 47 | [0.0.3]: https://github.com/nepalez/dry-initializer-rails/compare/v0.0.2...v0.0.3 48 | [1.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v0.0.3...v1.0.0 49 | [2.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v1.0.0...v2.0.0 50 | [3.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v2.0.0...v3.0.0 51 | [3.1.0]: https://github.com/nepalez/dry-initializer-rails/compare/v3.0.0...v3.1.0 52 | [3.1.1]: https://github.com/nepalez/dry-initializer-rails/compare/v3.1.0...v3.1.1 53 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Specify your gem"s dependencies in dry-initializer.gemspec 4 | gemspec 5 | 6 | group :development, :test do 7 | gem "pry", platform: :mri 8 | gem "pry-byebug", platform: :mri 9 | end 10 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | guard :rspec, cmd: "bundle exec rspec" do 2 | watch(%r{^spec/.+_spec\.rb$}) 3 | watch(%r{^spec/(spec_helper|support)}) { "spec" } 4 | watch(%r{^lib/.+}) { "spec" } 5 | end 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Andrew Kozin (nepalez), Vladimir Kochnev (marshall-lee) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dry-initializer-rails [![Join the chat at https://gitter.im/dry-rb/chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dry-rb/chat) 2 | 3 | [![Gem Version](https://badge.fury.io/rb/dry-initializer-rails.svg)][gem] 4 | [![Build Status](https://travis-ci.org/nepalez/dry-initializer-rails.svg?branch=master)][travis] 5 | [![Code Climate](https://codeclimate.com/github/nepalez/dry-initializer-rails/badges/gpa.svg)][codeclimate] 6 | [![Test Coverage](https://codeclimate.com/github/nepalez/dry-initializer-rails/badges/coverage.svg)][coveralls] 7 | [![Inline docs](http://inch-ci.org/github/nepalez/dry-initializer-rails.svg?branch=master)][inchpages] 8 | 9 | [gem]: https://rubygems.org/gems/dry-initializer-rails 10 | [travis]: https://travis-ci.org/nepalez/dry-initializer-rails 11 | [codeclimate]: https://codeclimate.com/github/nepalez/dry-initializer-rails 12 | [coveralls]: https://coveralls.io/r/nepalez/dry-initializer-rails 13 | [inchpages]: http://inch-ci.org/github/nepalez/dry-initializer-rails 14 | 15 | Rails plugin to [dry-initializer][dry-initializer] 16 | 17 | [dry-initializer]: https://github.com/dry-rb/dry-initializer 18 | 19 | ## Installation 20 | 21 | Add this line to your application's Gemfile: 22 | 23 | ```ruby 24 | gem 'dry-initializer-rails' 25 | ``` 26 | 27 | And then execute: 28 | 29 | ```shell 30 | $ bundle 31 | ``` 32 | 33 | Or install it yourself as: 34 | 35 | ```shell 36 | $ gem install dry-initializer-rails 37 | ``` 38 | 39 | ## Synopsis 40 | 41 | The gem provides value coercion to ActiveRecord instances. 42 | 43 | Add the `:model` setting to `param` or `option`: 44 | 45 | ```ruby 46 | require 'dry-initializer' 47 | require 'dry-initializer-rails' 48 | 49 | class CreateOrder 50 | extend Dry::Initializer 51 | 52 | # Params and options 53 | param :customer, model: 'Customer' # use either a name 54 | option :product, model: Product # or a class 55 | option :coupon, model: Coupon.where(active: true) # or relation 56 | 57 | def call 58 | Order.create customer: customer, product: product, coupon: coupon 59 | end 60 | end 61 | ``` 62 | 63 | Now you can assign values as pre-initialized model instances: 64 | 65 | ```ruby 66 | customer = Customer.create(id: 1) 67 | product = Product.create(id: 2) 68 | coupon = Coupon.create(id: 3, active: true) 69 | 70 | order = CreateOrder.new(customer, product: product, coupon: coupon).call 71 | order.customer # => # 72 | order.product # => # 73 | order.coupon # => # 74 | ``` 75 | 76 | ...or their ids: 77 | 78 | ```ruby 79 | order = CreateOrder.new(1, product: 2, coupon: 3).call 80 | order.customer # => # 81 | order.product # => # 82 | order.coupon # => # 83 | ``` 84 | 85 | The instance is envoked using method `find_by!(id: ...)`. 86 | 87 | ```ruby 88 | order = CreateOrder.new(1, product: 2, coupon: 4).call 89 | # raises # 90 | ``` 91 | 92 | You can specify custom `key` for searching model instance: 93 | 94 | ```ruby 95 | require 'dry-initializer-rails' 96 | 97 | class CreateOrder 98 | extend Dry::Initializer 99 | 100 | param :customer, model: 'User', find_by: 'name' 101 | option :product, model: Item, find_by: :name 102 | end 103 | ``` 104 | 105 | This time you can pass names (not ids) to the initializer: 106 | 107 | ```ruby 108 | order = CreateOrder.new('Andrew', product: 'the_thing_no_123').call 109 | 110 | order.customer # => 111 | order.product # => 112 | ``` 113 | 114 | ## Compatibility 115 | 116 | Tested under [MRI 2.2+](.travis.yml). 117 | 118 | ## Contributing 119 | 120 | * [Fork the project](https://github.com/nepalez/dry-initializer-rails) 121 | * Create your feature branch (`git checkout -b my-new-feature`) 122 | * Add tests for it 123 | * Commit your changes (`git commit -am '[UPDATE] Add some feature'`) 124 | * Push to the branch (`git push origin my-new-feature`) 125 | * Create a new Pull Request 126 | 127 | ## License 128 | 129 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 130 | 131 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | Bundler::GemHelper.install_tasks 3 | 4 | require "rspec/core/rake_task" 5 | 6 | # Adds dummy:db tasks. 7 | load "spec/dummy/Rakefile" 8 | 9 | # Declares gem's own tasks. 10 | desc "Runs test suite." 11 | task default: %w[dummy:db:migrate] do 12 | system "bundle exec rspec spec" 13 | end 14 | -------------------------------------------------------------------------------- /dry-initializer-rails.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |gem| 2 | gem.name = "dry-initializer-rails" 3 | gem.version = "3.1.1" 4 | gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"] 5 | gem.email = ["andrew.kozin@gmail.com"] 6 | gem.homepage = "https://github.com/nepalez/dry-initializer-rails" 7 | gem.summary = "Adds ActiveRecord-specific methods to Dry::Initializer" 8 | gem.license = "MIT" 9 | 10 | gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) 11 | gem.test_files = gem.files.grep(/^spec/) 12 | gem.extra_rdoc_files = Dir["README.md", "LICENSE", "CHANGELOG.md"] 13 | 14 | gem.required_ruby_version = ">= 2.3" 15 | 16 | gem.add_runtime_dependency "rails", "> 3.0" 17 | gem.add_runtime_dependency "dry-initializer", ">= 2.4", "< 4" 18 | 19 | gem.add_development_dependency "rspec", "~> 3.0" 20 | gem.add_development_dependency "rake", "> 10.0" 21 | gem.add_development_dependency "sqlite3", "~> 1.3" 22 | gem.add_development_dependency "database_cleaner", "~> 1.5" 23 | gem.add_development_dependency "rubocop", "~> 0.52" 24 | end 25 | -------------------------------------------------------------------------------- /lib/dry-initializer-rails.rb: -------------------------------------------------------------------------------- 1 | require_relative "dry/initializer/rails" 2 | -------------------------------------------------------------------------------- /lib/dry/initializer/rails.rb: -------------------------------------------------------------------------------- 1 | require "dry-initializer" 2 | require "rails" 3 | 4 | # Define a dispatcher for `:model` and `:find_by` options 5 | rails_dispatcher = lambda do |model: nil, find_by: :id, **options| 6 | return options unless model 7 | 8 | model = eval(model) if model.is_a? String 9 | klass = model.is_a?(ActiveRecord::Relation) ? model.klass : model 10 | 11 | coercer = lambda do |value| 12 | return value if value.nil? || value.is_a?(klass) 13 | model.find_by! find_by => value 14 | end 15 | 16 | options.merge(type: coercer) 17 | end 18 | 19 | # Register a dispatcher 20 | Dry::Initializer::Dispatchers << rails_dispatcher 21 | -------------------------------------------------------------------------------- /spec/dry/initializer/assignment_by_custom_key_spec.rb: -------------------------------------------------------------------------------- 1 | describe "assignment by custom key" do 2 | context "with mixin syntax" do 3 | before do 4 | class Test::Order 5 | extend Dry::Initializer 6 | 7 | param :user, model: "User", find_by: "name" 8 | option :product, model: Item, find_by: :name 9 | end 10 | end 11 | 12 | let!(:user) { User.create name: "Dude" } 13 | let!(:item) { Item.create name: "The thing" } 14 | 15 | it "works when records are present" do 16 | subject = Test::Order.new("Dude", product: "The thing") 17 | 18 | expect(subject.user).to eql user 19 | expect(subject.product).to eql item 20 | end 21 | 22 | it "raises when records are absent" do 23 | Test::Order.new("Dude", product: "The thing") 24 | 25 | expect { Test::Order.new("Man", product: "The thing") } 26 | .to raise_error ActiveRecord::RecordNotFound 27 | 28 | expect { Test::Order.new("Dude", product: "Something strange") } 29 | .to raise_error ActiveRecord::RecordNotFound 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /spec/dry/initializer/assignment_by_id_spec.rb: -------------------------------------------------------------------------------- 1 | describe "assignment by id" do 2 | before do 3 | class Test::Order 4 | extend Dry::Initializer 5 | 6 | param :user, model: "User" 7 | option :product, model: Item 8 | end 9 | end 10 | 11 | let!(:user) { User.create name: "Dude" } 12 | let!(:item) { Item.create name: "The thing" } 13 | 14 | it "works when records are present" do 15 | subject = Test::Order.new(user.id, product: item.id) 16 | 17 | expect(subject.user).to eql user 18 | expect(subject.product).to eql item 19 | end 20 | 21 | it "raises when records are absent" do 22 | Test::Order.new(user.id, product: item.id) 23 | 24 | expect { Test::Order.new(0, product: item) } 25 | .to raise_error ActiveRecord::RecordNotFound 26 | 27 | expect { Test::Order.new(user, product: 0) } 28 | .to raise_error ActiveRecord::RecordNotFound 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /spec/dry/initializer/assignment_by_model_spec.rb: -------------------------------------------------------------------------------- 1 | describe "assignment by model" do 2 | before do 3 | class Test::Order 4 | extend Dry::Initializer 5 | 6 | param :user, model: "User" 7 | option :product, model: Item 8 | end 9 | end 10 | 11 | let!(:user) { User.create name: "Dude" } 12 | let!(:item) { Item.create name: "The thing" } 13 | 14 | it "works" do 15 | subject = Test::Order.new(user, product: item) 16 | 17 | expect(subject.user).to eql user 18 | expect(subject.product).to eql item 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/dry/initializer/assignment_by_nil_spec.rb: -------------------------------------------------------------------------------- 1 | describe "assignment by model" do 2 | before do 3 | class Test::Order 4 | extend Dry::Initializer 5 | 6 | param :user, model: "User" 7 | option :product, model: Item 8 | end 9 | end 10 | 11 | let!(:user) { User.create name: "Dude" } 12 | let!(:item) { Item.create name: "The thing" } 13 | 14 | it "works" do 15 | subject = Test::Order.new(nil, product: nil) 16 | 17 | expect(subject.user).to be_nil 18 | expect(subject.product).to be_nil 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/dry/initializer/base_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | describe "base syntax" do 2 | before do 3 | class Test::Foo 4 | extend Dry::Initializer 5 | 6 | param :foo 7 | param :bar 8 | option :baz 9 | option :qux 10 | end 11 | end 12 | 13 | it "supported" do 14 | subject = Test::Foo.new(1, 2, baz: 3, qux: 4) 15 | 16 | expect(subject.foo).to eql 1 17 | expect(subject.bar).to eql 2 18 | expect(subject.baz).to eql 3 19 | expect(subject.qux).to eql 4 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /spec/dry/initializer/modelling_relation_spec.rb: -------------------------------------------------------------------------------- 1 | describe "modelling relation" do 2 | before do 3 | class Test::Order 4 | extend Dry::Initializer 5 | 6 | param :user, model: "User.where(name: 'Dude')" 7 | option :product, model: Item.where(name: "The thing") 8 | end 9 | end 10 | 11 | let!(:user) { User.create id: 9, name: "Dude" } 12 | let!(:item) { Item.create id: 1, name: "The thing" } 13 | let!(:other) { Item.create id: 2, name: "Another thing" } 14 | 15 | it "uses relation" do 16 | subject = Test::Order.new(9, product: 1) 17 | 18 | expect(subject.user).to eq user 19 | expect(subject.product).to eq item 20 | end 21 | 22 | it "raises when record not found for the relation" do 23 | expect { Test::Order.new(9, product: 2) } 24 | .to raise_error(ActiveRecord::RecordNotFound) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | require "active_record" 2 | require "bundler/setup" 3 | 4 | namespace :dummy do 5 | task :environment do 6 | require_relative "lib/dummy" 7 | end 8 | 9 | namespace :db do 10 | task load_config: :environment 11 | end 12 | 13 | load "active_record/railties/databases.rake" 14 | end 15 | -------------------------------------------------------------------------------- /spec/dummy/app/models/item.rb: -------------------------------------------------------------------------------- 1 | class Item < ActiveRecord::Base 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ActiveRecord::Base 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/config/database.yml: -------------------------------------------------------------------------------- 1 | --- 2 | test: 3 | adapter: :sqlite3 4 | database: spec/dummy/db/test.sqlite3 5 | pool: 5 6 | timeout: 5000 7 | -------------------------------------------------------------------------------- /spec/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do |config| 2 | dummy = File.expand_path "../..", __FILE__ 3 | database_yml = File.join(dummy, "config/database.yml") 4 | 5 | config.database_configuration = YAML.load File.read(database_yml) 6 | config.db_dir = File.join(dummy, "db") 7 | config.env = :test 8 | config.migrations_paths = [File.join(dummy, "db/migrate")] 9 | config.root = dummy 10 | end 11 | -------------------------------------------------------------------------------- /spec/dummy/db/migrate/20160509134800_create_users.rb: -------------------------------------------------------------------------------- 1 | class CreateUsers < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :users do |t| 4 | t.string :name, index: true 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/dummy/db/migrate/20160509134900_create_items.rb: -------------------------------------------------------------------------------- 1 | class CreateItems < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :items do |t| 4 | t.string :name, index: true 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/dummy/db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(version: 20160509134900) do 14 | 15 | create_table "items", force: :cascade do |t| 16 | t.string "name" 17 | t.index ["name"], name: "index_items_on_name" 18 | end 19 | 20 | create_table "users", force: :cascade do |t| 21 | t.string "name" 22 | t.index ["name"], name: "index_users_on_name" 23 | end 24 | 25 | end 26 | -------------------------------------------------------------------------------- /spec/dummy/lib/dummy.rb: -------------------------------------------------------------------------------- 1 | require "active_record" 2 | require "bundler/setup" 3 | 4 | module Dummy 5 | require_relative "dummy/application" 6 | require_relative "../config/environment" 7 | require_relative "../app/models/item.rb" 8 | require_relative "../app/models/user.rb" 9 | end 10 | -------------------------------------------------------------------------------- /spec/dummy/lib/dummy/application.rb: -------------------------------------------------------------------------------- 1 | module Dummy 2 | class Application 3 | class << self 4 | # Configuration settings wrapper for the 5 | # ActiveRecord::Tasks::DatabaseTasks. 6 | # 7 | # Establishes AR connection after configuration. 8 | # 9 | def configure 10 | yield tasks 11 | base.configurations = tasks.database_configuration 12 | base.establish_connection(tasks.env) 13 | end 14 | 15 | private 16 | 17 | def base 18 | @base ||= ActiveRecord::Base 19 | end 20 | 21 | def tasks 22 | @tasks ||= ActiveRecord::Tasks::DatabaseTasks 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "dry-initializer-rails" 2 | require "pry" 3 | require "database_cleaner" 4 | 5 | require_relative "dummy/lib/dummy" 6 | 7 | DatabaseCleaner.strategy = :truncation 8 | 9 | RSpec.configure do |config| 10 | config.order = :random 11 | config.filter_run focus: true 12 | config.run_all_when_everything_filtered = true 13 | 14 | # Prepare the Test namespace for constants defined in specs 15 | config.around(:each) do |example| 16 | Test = Class.new(Module) 17 | example.run 18 | Object.send :remove_const, :Test 19 | DatabaseCleaner.clean 20 | end 21 | end 22 | --------------------------------------------------------------------------------