Permalink
Browse files

refactoring

* public API change! renamed streamlined_model to streamlined_ui
* reorganized controller context scope (created only once per controller)
* streamlined_ui can take a context, so different controllers can have different streamlined settings for the same model
  • Loading branch information...
1 parent c2cad41 commit 19b8b8d5893e796064d2777b91223b7cf4a84ae3 stu committed Feb 26, 2008
View
@@ -1,4 +1,4 @@
-Copyright (c) 2006-08 Relevance, LLC (http://relevancellc.com, http://streamlinedframework.org)
+Copyright (c) 2006-08 Relevance, Inc. (http://relevancellc.com, http://streamlinedframework.org)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished
View
1 TODO
@@ -1,3 +1,2 @@
-- review the @custom_model_name stuff -- does this make any sense? does it belong on ControllerContext?
- integrate association columns with acts_as_dropdown if present?
@@ -1,5 +1,5 @@
/* Streamlined.js
- * (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
+ * (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
*
* Streamlined.js is freely distributable under the terms of an MIT-style license.
* For details, see http://streamlinedframework.org/
@@ -1,5 +1,5 @@
# Streamlined
-# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
+# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
# Streamlined is freely distributable under the terms of an MIT-style license.
# For details, see http://streamlinedframework.org/
#
@@ -1,33 +1,50 @@
# per controller context, kept for the lifetime of the controller class
# and made available via delegation to controllers and views
class Streamlined::Context::ControllerContext
- attr_accessor :model_name
-
- DELEGATES = [:model_name,
- :model,
- :model_symbol,
- :model_table,
- :model_underscore,
- :model_ui,
- {:to=>:streamlined_controller_context}].freeze
-
+ attr_accessor :ui_model_name, :ui_context
+
+ def initialize(ui_model_name)
+ streamlined_ui(ui_model_name)
+ end
+
+ def self.delegates
+ [:model_name,
+ :model,
+ :model_symbol,
+ :model_table,
+ :model_underscore,
+ :model_ui,
+ :streamlined_ui]
+ end
+
def model
- @model ||= Class.class_eval(model_name)
+ Class.class_eval(model_name)
end
def model_symbol
- @model_symbol ||= Inflector.underscore(model_name).to_sym
+ Inflector.underscore(model_name).to_sym
end
def model_table
- @model_table ||= Inflector.tableize(model_name)
+ Inflector.tableize(model_name)
end
def model_underscore
- @model_underscore ||= Inflector.underscore(model_name)
+ Inflector.underscore(model_name)
end
def model_ui
- Streamlined.ui_for(model_name)
+ Streamlined.ui_for(ui_model_name, :context => ui_context)
+ end
+
+ def model_name
+ ui_model_name
end
+
+ def streamlined_ui(ui_model_name, ui_context = nil, &blk)
+ @ui_model_name = ui_model_name.to_s
+ @ui_context = ui_context
+ model_ui.instance_eval(&blk) if block_given?
+ end
+
end
@@ -1,5 +1,5 @@
# Streamlined
-# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
+# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
# Streamlined is freely distributable under the terms of an MIT-style license.
# For details, see http://streamlinedframework.org/
module Streamlined::Controller; end
@@ -47,26 +47,10 @@ def instance=(value)
private
- def initialize_with_streamlined_variables
- initialize_streamlined_values
- streamlined_logger.info("model NAME: #{model_name}")
- streamlined_logger.info("model: #{model.inspect}")
- end
-
def initialize_request_context
@streamlined_request_context = Streamlined::Context::RequestContext.new(params[:page_options])
end
- def initialize_streamlined_values
- @streamlined_controller_context = Streamlined::Context::ControllerContext.new
- @streamlined_controller_context.model_name = self.class.model_name || Inflector.classify(self.class.controller_name)
- # TODO: why isn't this in the html head?
- @page_title = "Manage #{model_name.pluralize}"
- rescue Exception => ex
- streamlined_logger.info("Could not instantiate controller: #{self.class.name}")
- raise ex
- end
-
# rewrite of rails method
def paginator_and_collection_for(collection_id, options) #:nodoc:
klass = model
@@ -87,21 +71,13 @@ def streamlined_logger
end
module Streamlined::Controller::ClassMethods
- @custom_model_name = nil
-
def acts_as_streamlined(options = {})
- raise ArgumentError, "options[:helpers] is deprecated" if options[:helpers]
class_eval do
- attr_reader :streamlined_controller_context, :streamlined_request_context
+ attr_reader :streamlined_request_context
attr_with_default(:breadcrumb_trail) {[]}
helper_method :crud_context, :render_tabs, :render_partials, :instance, :breadcrumb_trail
- # delegated helpers do not appear as routable actions!
- def self.delegate_non_routable(*delegates_args)
- delegates *delegates_args
- delegates_args.each {|arg| hide_action(arg)}
- end
- delegate_non_routable(*Streamlined::Context::ControllerContext::DELEGATES)
- delegate_non_routable(*Streamlined::Context::RequestContext::DELEGATES)
+ delegate_non_routable(*Streamlined::Context::RequestContext::DELEGATES)
+ initialize_streamlined_controller_context(controller_name.singularize.classify)
include Streamlined::Controller::InstanceMethods
before_filter :initialize_request_context
Dir["#{RAILS_ROOT}/app/streamlined/*.rb"].each do |name|
@@ -110,18 +86,26 @@ def self.delegate_non_routable(*delegates_args)
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
- alias_method_chain :initialize, :streamlined_variables
end
end
-
- def model_name
- @custom_model_name || nil
+
+ def delegate_non_routable(*delegates_args)
+ delegates *delegates_args
+ delegates_args.each {|arg| hide_action(arg)}
end
- def streamlined_model(mod)
- @custom_model_name = mod.instance_of?(String) ? mod : mod.name
+ # controller name is passed in by acts_as_streamlined and becomes model name
+ def initialize_streamlined_controller_context(model_name)
+ class << self
+ attr_reader :streamlined_controller_context
+ delegate *Streamlined::Context::ControllerContext.delegates +
+ [{:to => :streamlined_controller_context}]
+ end
+ @streamlined_controller_context = Streamlined::Context::ControllerContext.new(model_name)
+ delegate_non_routable *Streamlined::Context::ControllerContext.delegates +
+ [{:to => "self.class"}]
end
-
+
def filters
@filters ||= {}
end
View
@@ -1,5 +1,5 @@
# Streamlined
-# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
+# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
# Streamlined is freely distributable under the terms of an MIT-style license.
# For details, see http://streamlinedframework.org/
#
@@ -27,9 +27,14 @@ module Streamlined::Helper
def self.included(includer)
includer.class_eval do
- attr_reader :streamlined_controller_context, :streamlined_request_context
- # TODO: should delegate to controller, not controller context. Need to test. --SDH
- delegates *Streamlined::Context::ControllerContext::DELEGATES
+ attr_reader :streamlined_request_context
+ delegates :model_name,
+ :model,
+ :model_symbol,
+ :model_table,
+ :model_underscore,
+ :model_ui,
+ {:to=>:controller}
delegates :list_columns, :to=>:model_ui
end
end
View
@@ -1,5 +1,5 @@
# Streamlined
-# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
+# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
# Streamlined is freely distributable under the terms of an MIT-style license.
# For details, see http://streamlinedframework.org/
@@ -19,7 +19,6 @@ def setup
it "delegated methods are not routable" do
action_methods = PeopleController.action_methods.map(&:to_sym)
(action_methods & Streamlined::Context::RequestContext::DELEGATES).size.should == 0
- (action_methods & Streamlined::Context::ControllerContext::DELEGATES).size.should == 0
end
it "should render index" do
@@ -4,18 +4,20 @@
describe "Streamlined::Context::ControllerContext" do
def setup
- @context = Streamlined::Context::ControllerContext.new
- @context.model_name = "String"
+ @context = Streamlined::Context::ControllerContext.new(String)
end
it "model ui" do
assert_instance_of Streamlined::UI, @context.model_ui
- context2 = Streamlined::Context::ControllerContext.new
- context2.model_name = "Integer"
+ context2 = Streamlined::Context::ControllerContext.new("Integer")
assert_not_equal context2.model_ui, @context.model_ui, "every model class gets its own anonymous subclass for ui"
end
it "model ui uses passed model class" do
assert_equal String, @context.model_ui.model
+ end
+
+ it "stringifies model class passed in, so callers can pass class or string" do
+ assert_equal "String", @context.ui_model_name
end
end
@@ -8,36 +8,24 @@ class FooController < ApplicationController
describe "Streamlined::Controller" do
include Streamlined::Controller::ClassMethods
- # verify that exception is logged and rethrown
- it "initialize with streamlined variables" do
- o = Object.new
- o.extend Streamlined::Controller::InstanceMethods
- logger = flexmock("logger") do |mock|
- mock.should_receive(:info).once
- end
- flexmock(o, :streamlined_logger => logger)
- flexmock(Streamlined::Context::ControllerContext).should_receive(:new).and_raise(RuntimeError,"mocked!")
- assert_raise(RuntimeError) do
- o.send :initialize_streamlined_values
- end
+ before do
+ @clazz = Class.new
+ @clazz.extend Streamlined::Controller::ClassMethods
end
- it "deprecation of helper overrides" do
- c = FooController
- c.acts_as_streamlined
- assert_nil c.send(:instance_variable_get, :@helper_overrides)
- assert_raises(ArgumentError) do
- c.acts_as_streamlined :helpers => ["NEW HELPER"]
- end
+ it "initialize streamlined controller context" do
+ @clazz.expects(:delegate_non_routable)
+ @clazz.initialize_streamlined_controller_context("Foo")
+ context = @clazz.streamlined_controller_context
+ context.should.be.instance_of Streamlined::Context::ControllerContext
end
it "streamlined model" do
- streamlined_model("Test")
- assert_equal "Test", model_name
- streamlined_model(stub(:name => "Tim"))
- assert_equal "Tim",
- model_name,
- "streamlined_model should extract name property"
+ @clazz.expects(:delegate_non_routable)
+ @clazz.initialize_streamlined_controller_context("Foo")
+ @clazz.model_name.should == "Foo"
+ @clazz.streamlined_ui("Bar")
+ @clazz.model_name.should == "Bar"
end
it "render filter" do

0 comments on commit 19b8b8d

Please sign in to comment.