Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sifters don't work on on-the-fly tables #94

Open
pelletencate opened this issue Jun 13, 2018 · 0 comments
Open

Sifters don't work on on-the-fly tables #94

pelletencate opened this issue Jun 13, 2018 · 0 comments

Comments

@pelletencate
Copy link

pelletencate commented Jun 13, 2018

Issue

BabySqueel[:table] returns an object that does not respond to sift.

Now that I'm writing this, I realize it makes some sense, BabySqueel doesn't know anything about which model belongs to a table. However, would it be possible to add a model's sifter to a BabySqueel-initiated table object?

Reproduction

require 'bundler/inline'
require 'minitest/spec'
require 'minitest/autorun'

gemfile true do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 5.0.0' # which Active Record version?
  gem 'sqlite3'
  gem 'baby_squeel', github: 'rzane/baby_squeel'
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :dogs, force: true do |t|
    t.string :name
  end

  create_table :people, force: true do |t|
    t.string :name
    t.integer :age
    t.string :dog_name
  end
end

class Dog < ActiveRecord::Base
end

class Person < ActiveRecord::Base
  sifter(:female) { name.in? %w(angela brittany carol) }
end

class BabySqueelTest < Minitest::Spec
  it 'does not raise an exception' do
    people = BabySqueel[:people]
    # Find all dogs that have a female adult boss
    scope = Dog.joining { people.on(people.dog_name == name) }.where.has { people.sift(:female) & (people.age >= 18) }

	# FIXME: This SQL may be off, the point is whether or not it raises an exception. I'm not an expert with minitest.
    scope.to_sql.must_equal %{
      SELECT "dogs".* FROM "dogs"
        INNER JOIN "people" ON "people"."dog_name" = "dogs"."name"
        WHERE "people"."name" IN ('angela', 'brittany', 'carol')
          AND "people"."age" >= 18;
    }.squish
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant