Last active
October 14, 2017 15:31
-
-
Save twelve17/e318aa8953ae3bc79e65b37218389e07 to your computer and use it in GitHub Desktop.
order_query - sorting by association
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'active_record' | |
require 'order_query' | |
require 'pry-byebug' | |
RSpec.describe OrderQuery do | |
class Employee < ActiveRecord::Base | |
include OrderQuery | |
has_one :contract | |
end | |
class Contract < ActiveRecord::Base | |
belongs_to :employee | |
end | |
def seed_data | |
updated_at = Time.now | |
employees = [{ | |
name: "John", | |
email: "[email protected]", | |
ctype: "seasonal" | |
}, | |
{ | |
name: "Fred", | |
email: "[email protected]", | |
ctype: "annual" | |
}, | |
].map do |info| | |
contract_type = info.delete(:ctype) | |
emp = Employee.create!(info) | |
emp.create_contract!(contract_type: contract_type, start_at: updated_at) | |
updated_at += 60*60*24 | |
binding.pry | |
emp | |
end | |
end | |
def db_config | |
@db_config ||= { | |
adapter: 'mysql2', | |
host: ENV.fetch('DB_HOST'), | |
port: ENV.fetch('DB_PORT', 3306), | |
username: ENV.fetch('DB_USERNAME'), | |
password: ENV.fetch('DB_PASSWORD'), | |
database: ENV.fetch('DB_NAME') | |
} | |
@db_config.dup | |
end | |
def establish_connection(db_name=nil) | |
config = db_config | |
if db_name | |
config[:database] = db_name | |
end | |
ActiveRecord::Base.establish_connection(config) | |
ActiveRecord::Base.connection | |
end | |
def create_tables | |
establish_connection | |
ActiveRecord::Schema.define do | |
self.verbose = false | |
create_table :employees do |t| | |
t.column :name, :string | |
t.column :email, :string | |
end | |
create_table :contracts do |t| | |
t.column :employee_id, :bigint, null:false | |
t.column :contract_type, :string | |
t.column :start_at, :datetime | |
t.index ["employee_id"], name: "index_contracts_on_employee_id", using: :btree | |
end | |
add_foreign_key "contracts", "employees" | |
end | |
Employee.reset_column_information | |
end | |
def drop_tables | |
[:contracts, :employees].each do |t| | |
begin | |
establish_connection.drop_table t | |
rescue ActiveRecord::StatementInvalid | |
end | |
end | |
end | |
def create_db | |
establish_connection(:mysql).execute("CREATE DATABASE IF NOT EXISTS #{db_config[:database]}") | |
end | |
def drop_db | |
establish_connection(:mysql).execute("DROP DATABASE IF EXISTS #{db_config[:database]}") | |
end | |
describe "search" do | |
before :all do | |
create_db | |
drop_tables | |
create_tables | |
seed_data | |
end | |
after :all do | |
drop_tables | |
drop_db | |
end | |
describe "sort on secondary join table" do | |
it "can fetch 'next' on a Employee point when sorting by contract.contract_type" do | |
space = Employee.joins(:contract).seek([:contract_type, :desc, "contract.contract_type"]) | |
expect(space.class).to be(OrderQuery::Space) | |
record = space.at(Employee.first).after(false).limit(1) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment