Sha256: 377a51b94ff41fd801860e4198edf5e5b9eb639f625b63f2e2aad9a4c2390096

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'rubygems'
require "rails"
require "rails/test_help"
require "active_support/core_ext"
require "turn"
require 'set_builder'



# Mocks NamedScope so that composed scoping can be tested

class FakeScope
  
  def initialize(args)
    args = [args] unless args.is_a?(Array)
    @composed_scope = args
  end
  
  def scoped(args)
    FakeScope.new(@composed_scope + [args])
  end
  
  def composed_scope
    @composed_scope
  end
  
end



# Sample class used by tests

SetBuilder::ValueMap.register(:school, [[1, "Concordia"], [2, "McKendree"]])

class Friend
  extend SetBuilder


  trait(:is, "awesome") do |query|
    {:conditions => {:awesome => true}}
  end

  trait(nil, "died") do |query|
    {:conditions => {:alive => false}}
  end

  # this trait accepts modifiers --- an adverbial clause
  trait(:was, "born", :date) do |query|
    {:conditions => query.modifiers[0].build_conditions_for("friends.birthday")}
  end

  trait(:whose, "age", :number) do |query|
    {:conditions => query.modifiers[0].build_conditions_for("friends.age")}
  end

  # this trait has a direct object
  trait(:has, {"attended" => :school}) do |query|
    {
      :joins => "INNER JOIN schools ON friends.school_id=schools.id",
      :conditions => {"schools.id" => query.direct_object}
    }
  end

  # this trait is a noun
  # also modifiers can be classes
  trait(:whose, "name", StringModifier) do |query|
    {:conditions => query.modifiers[0].build_conditions_for("friends.name")}
  end
  
  
  
  # by stubbing out scoped, we can unit test the `performed` features
  def self.scoped(*args)
    FakeScope.new(args)
  end
  
  
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
set_builder-1.1.0 test/test_helper.rb
set_builder-1.0.2 test/test_helper.rb