Skip to content

Commit

Permalink
Pass options from scopes block to each individual scope
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed May 16, 2024
1 parent 5b3d9ce commit 103c7b5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/trestle/resource/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def count(&block)

def scopes(options={}, &block)
admin.scopes.options.merge!(options)
admin.scopes.append(&block) if block_given?
admin.scopes.append(options, &block) if block_given?
end

def scope(name, scope=nil, options={}, &block)
Expand Down
14 changes: 7 additions & 7 deletions lib/trestle/scopes/block.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module Trestle
class Scopes
class Block
attr_reader :block
attr_reader :block, :options

def initialize(&block)
@block = block
def initialize(options={}, &block)
@options, @block = options, block
end

# Evaluates the scope block within the given admin context
# and returns an array of the scopes that were defined.
def scopes(context)
context = Evaluator.new(context)
context = Evaluator.new(context, options)
context.instance_exec(context, &block)
context.scopes
end
Expand All @@ -20,8 +20,8 @@ class Evaluator

attr_reader :scopes

def initialize(context=nil)
@context = context
def initialize(context=nil, defaults={})
@context, @defaults = context, defaults
@scopes = []
end

Expand All @@ -31,7 +31,7 @@ def scope(name, scope=nil, options={}, &block)
scope = nil
end

scopes << Scope.new(@context, name, options, &(scope || block))
scopes << Scope.new(@context, name, @defaults.merge(options), &(scope || block))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/trestle/scopes/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize
@options = {}
end

def append(&block)
@blocks << Block.new(&block)
def append(options={}, &block)
@blocks << Block.new(options, &block)
end

# Evaluates each of the scope blocks within the given admin context
Expand Down
36 changes: 36 additions & 0 deletions spec/trestle/scopes/block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Trestle::Scopes::Block do
let(:admin) { double }
let(:options) { {} }

subject(:block) do
Trestle::Scopes::Block.new(options) do
scope :first
scope :second, count: false
end
end

describe "#scopes" do
it "returns scopes that have been defined within the block" do
scope1, scope2 = block.scopes(admin)

expect(scope1.name).to eq(:first)
expect(scope1.options).to eq({})

expect(scope2.name).to eq(:second)
expect(scope2.options).to eq({ count: false })
end
end

context "with options" do
let(:options) { { count: false } }

it "applies the block options to each scope" do
scope1, scope2 = block.scopes(admin)

expect(scope1.options).to eq({ count: false })
expect(scope2.options).to eq({ count: false })
end
end
end

0 comments on commit 103c7b5

Please sign in to comment.