Skip to content

Commit

Permalink
! url_for, Rails accessing @controller directly. Bad!
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Jul 19, 2010
1 parent 393637b commit edad444
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions gem/lib/rails2/lib/view_models/base.rb
Expand Up @@ -10,6 +10,17 @@ class Base
#
include ActionController::Helpers

# This is really only needed because some Rails helpers access
# @controller directly.
# It's really bad.
#
# TODO Make it call super.
#
def initialize model, controller_or_view
@model = model
@context = @controller = ContextExtractor.new(controller_or_view).extract
end

class << self

# Alias the context_method to the rails-centric controller_method.
Expand Down
2 changes: 2 additions & 0 deletions gem/lib/shared/lib/view_models/base.rb
Expand Up @@ -14,6 +14,8 @@ class Base
# Create a view_model. To create a view_model, you need to have a model (to present) and a context.
# The context is usually a view, a controller, or an app, but doesn't need to be.
#
# TODO Include this.
#
def initialize model, app_or_controller_or_view
@model = model
@context = ContextExtractor.new(app_or_controller_or_view).extract
Expand Down
5 changes: 5 additions & 0 deletions rails235/app/view_models/book.rb
Expand Up @@ -3,6 +3,11 @@ class ViewModels::Book < ViewModels::Item
model_reader :pages

def description
# This is to test the url_for helper.
#
# Note: Needed because for example Rails accesses @controller, which shared/base does not provide.
#
url_for self
"It has #{pages} pages."
end

Expand Down
2 changes: 2 additions & 0 deletions rails235/app/view_models/item.rb
@@ -1,6 +1,8 @@
class ViewModels::Item < ViewModels::Base

helper ERB::Util
helper ActionView::Helpers::TagHelper
helper ActionView::Helpers::UrlHelper

model_reader :name, :filter_through => :html_escape

Expand Down
2 changes: 1 addition & 1 deletion rails235/config/initializers/view_models_gem.rb
@@ -1,3 +1,3 @@
# For development purposes.
#
# require File.join(File.dirname(__FILE__), '/../../../gem/init.rb')
require File.join(File.dirname(__FILE__), '/../../../gem/lib/rails2/init.rb')
6 changes: 5 additions & 1 deletion rails235/config/routes.rb
@@ -1,6 +1,10 @@
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.


map.resource :view_models do |vm|
vm.resource :book
end

# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
Expand Down

0 comments on commit edad444

Please sign in to comment.