Skip to content

Commit

Permalink
Merge pull request #9 from dblandin/master
Browse files Browse the repository at this point in the history
Add configuration block support to open method
  • Loading branch information
clayallsopp committed May 6, 2013
2 parents f0874b5 + 08b758d commit a52c109
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md 100644 → 100755
Expand Up @@ -96,3 +96,16 @@ class AppDelegate
end
end
```

## Configuration of View Controllers

If you need to configure a view controller before the router navigates to it, use a block:

``` ruby
# Configure and push an ImageEditorController
BW::Device.camera.any.picture(media_types: [:movie, :image]) do |result|
router.open('editor') do |controller|
controller.image = result[:original_image]
end
end
```
9 changes: 8 additions & 1 deletion lib/routable/router.rb 100644 → 100755
Expand Up @@ -83,7 +83,7 @@ def open_external(url)
# EX
# router.open("users/3")
# => router.navigation_controller pushes a UsersController
def open(url, animated = true)
def open(url, animated = true, &block)
controller_options = options_for_url(url)

if controller_options[:callback]
Expand All @@ -99,6 +99,13 @@ def open(url, animated = true)
end

controller = controller_for_url(url)

# Allow configuration of the view controller after
# initialization but before navigating to the view controller
if block_given?
block.call(controller)
end

if self.navigation_controller.modalViewController
dismiss_animated = animated

Expand Down
25 changes: 25 additions & 0 deletions spec/main_spec.rb 100644 → 100755
Expand Up @@ -14,6 +14,16 @@ class NoParamsController < UIViewController
class LoginController < UIViewController
end

class ConfigurableController < UIViewController
attr_accessor :configured

def init
super.tap do |controller|
controller.configured = false
end
end
end

describe "the url router" do
before do
@router = Routable::Router.new
Expand All @@ -23,6 +33,21 @@ class LoginController < UIViewController
@nav_controller.viewControllers.count.should == 0
end

describe '#open' do
it 'accepts a configuration block' do
url = 'configured'
@router.navigation_controller = @nav_controller
@router.map(url, ConfigurableController, shared: true)

@router.open(url) do |controller|
controller.configured = true
end

controller = @router.controller_for_url(url)
controller.configured.should.be.true
end
end

def make_test_controller_route
format = "users/:user_id"
@router.map(format, UsersTestController)
Expand Down

0 comments on commit a52c109

Please sign in to comment.