Skip to content

Commit 19b8b8d

Browse files
author
stu
committed
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
1 parent c2cad41 commit 19b8b8d

File tree

11 files changed

+83
-89
lines changed

11 files changed

+83
-89
lines changed

MIT-LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2006-08 Relevance, LLC (http://relevancellc.com, http://streamlinedframework.org)
1+
Copyright (c) 2006-08 Relevance, Inc. (http://relevancellc.com, http://streamlinedframework.org)
22

33
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,
44
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

TODO

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
- review the @custom_model_name stuff -- does this make any sense? does it belong on ControllerContext?
21
- integrate association columns with acts_as_dropdown if present?
32

files/public/javascripts/streamlined.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Streamlined.js
2-
* (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
2+
* (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
33
*
44
* Streamlined.js is freely distributable under the terms of an MIT-style license.
55
* For details, see http://streamlinedframework.org/

lib/relevance/active_record_extensions.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Streamlined
2-
# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
2+
# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
33
# Streamlined is freely distributable under the terms of an MIT-style license.
44
# For details, see http://streamlinedframework.org/
55
#
+32-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
# per controller context, kept for the lifetime of the controller class
22
# and made available via delegation to controllers and views
33
class Streamlined::Context::ControllerContext
4-
attr_accessor :model_name
5-
6-
DELEGATES = [:model_name,
7-
:model,
8-
:model_symbol,
9-
:model_table,
10-
:model_underscore,
11-
:model_ui,
12-
{:to=>:streamlined_controller_context}].freeze
13-
4+
attr_accessor :ui_model_name, :ui_context
5+
6+
def initialize(ui_model_name)
7+
streamlined_ui(ui_model_name)
8+
end
9+
10+
def self.delegates
11+
[:model_name,
12+
:model,
13+
:model_symbol,
14+
:model_table,
15+
:model_underscore,
16+
:model_ui,
17+
:streamlined_ui]
18+
end
19+
1420
def model
15-
@model ||= Class.class_eval(model_name)
21+
Class.class_eval(model_name)
1622
end
1723

1824
def model_symbol
19-
@model_symbol ||= Inflector.underscore(model_name).to_sym
25+
Inflector.underscore(model_name).to_sym
2026
end
2127

2228
def model_table
23-
@model_table ||= Inflector.tableize(model_name)
29+
Inflector.tableize(model_name)
2430
end
2531

2632
def model_underscore
27-
@model_underscore ||= Inflector.underscore(model_name)
33+
Inflector.underscore(model_name)
2834
end
2935

3036
def model_ui
31-
Streamlined.ui_for(model_name)
37+
Streamlined.ui_for(ui_model_name, :context => ui_context)
38+
end
39+
40+
def model_name
41+
ui_model_name
3242
end
43+
44+
def streamlined_ui(ui_model_name, ui_context = nil, &blk)
45+
@ui_model_name = ui_model_name.to_s
46+
@ui_context = ui_context
47+
model_ui.instance_eval(&blk) if block_given?
48+
end
49+
3350
end

lib/streamlined/controller.rb

+19-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Streamlined
2-
# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
2+
# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
33
# Streamlined is freely distributable under the terms of an MIT-style license.
44
# For details, see http://streamlinedframework.org/
55
module Streamlined::Controller; end
@@ -47,26 +47,10 @@ def instance=(value)
4747

4848

4949
private
50-
def initialize_with_streamlined_variables
51-
initialize_streamlined_values
52-
streamlined_logger.info("model NAME: #{model_name}")
53-
streamlined_logger.info("model: #{model.inspect}")
54-
end
55-
5650
def initialize_request_context
5751
@streamlined_request_context = Streamlined::Context::RequestContext.new(params[:page_options])
5852
end
5953

60-
def initialize_streamlined_values
61-
@streamlined_controller_context = Streamlined::Context::ControllerContext.new
62-
@streamlined_controller_context.model_name = self.class.model_name || Inflector.classify(self.class.controller_name)
63-
# TODO: why isn't this in the html head?
64-
@page_title = "Manage #{model_name.pluralize}"
65-
rescue Exception => ex
66-
streamlined_logger.info("Could not instantiate controller: #{self.class.name}")
67-
raise ex
68-
end
69-
7054
# rewrite of rails method
7155
def paginator_and_collection_for(collection_id, options) #:nodoc:
7256
klass = model
@@ -87,21 +71,13 @@ def streamlined_logger
8771
end
8872

8973
module Streamlined::Controller::ClassMethods
90-
@custom_model_name = nil
91-
9274
def acts_as_streamlined(options = {})
93-
raise ArgumentError, "options[:helpers] is deprecated" if options[:helpers]
9475
class_eval do
95-
attr_reader :streamlined_controller_context, :streamlined_request_context
76+
attr_reader :streamlined_request_context
9677
attr_with_default(:breadcrumb_trail) {[]}
9778
helper_method :crud_context, :render_tabs, :render_partials, :instance, :breadcrumb_trail
98-
# delegated helpers do not appear as routable actions!
99-
def self.delegate_non_routable(*delegates_args)
100-
delegates *delegates_args
101-
delegates_args.each {|arg| hide_action(arg)}
102-
end
103-
delegate_non_routable(*Streamlined::Context::ControllerContext::DELEGATES)
104-
delegate_non_routable(*Streamlined::Context::RequestContext::DELEGATES)
79+
delegate_non_routable(*Streamlined::Context::RequestContext::DELEGATES)
80+
initialize_streamlined_controller_context(controller_name.singularize.classify)
10581
include Streamlined::Controller::InstanceMethods
10682
before_filter :initialize_request_context
10783
Dir["#{RAILS_ROOT}/app/streamlined/*.rb"].each do |name|
@@ -110,18 +86,26 @@ def self.delegate_non_routable(*delegates_args)
11086
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
11187
verify :method => :post, :only => [ :destroy, :create, :update ],
11288
:redirect_to => { :action => :list }
113-
alias_method_chain :initialize, :streamlined_variables
11489
end
11590
end
116-
117-
def model_name
118-
@custom_model_name || nil
91+
92+
def delegate_non_routable(*delegates_args)
93+
delegates *delegates_args
94+
delegates_args.each {|arg| hide_action(arg)}
11995
end
12096

121-
def streamlined_model(mod)
122-
@custom_model_name = mod.instance_of?(String) ? mod : mod.name
97+
# controller name is passed in by acts_as_streamlined and becomes model name
98+
def initialize_streamlined_controller_context(model_name)
99+
class << self
100+
attr_reader :streamlined_controller_context
101+
delegate *Streamlined::Context::ControllerContext.delegates +
102+
[{:to => :streamlined_controller_context}]
103+
end
104+
@streamlined_controller_context = Streamlined::Context::ControllerContext.new(model_name)
105+
delegate_non_routable *Streamlined::Context::ControllerContext.delegates +
106+
[{:to => "self.class"}]
123107
end
124-
108+
125109
def filters
126110
@filters ||= {}
127111
end

lib/streamlined/helper.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Streamlined
2-
# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
2+
# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
33
# Streamlined is freely distributable under the terms of an MIT-style license.
44
# For details, see http://streamlinedframework.org/
55
#
@@ -27,9 +27,14 @@ module Streamlined::Helper
2727

2828
def self.included(includer)
2929
includer.class_eval do
30-
attr_reader :streamlined_controller_context, :streamlined_request_context
31-
# TODO: should delegate to controller, not controller context. Need to test. --SDH
32-
delegates *Streamlined::Context::ControllerContext::DELEGATES
30+
attr_reader :streamlined_request_context
31+
delegates :model_name,
32+
:model,
33+
:model_symbol,
34+
:model_table,
35+
:model_underscore,
36+
:model_ui,
37+
{:to=>:controller}
3338
delegates :list_columns, :to=>:model_ui
3439
end
3540
end

lib/streamlined/ui.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Streamlined
2-
# (c) 2005-7 Relevance, LLC. (http://thinkrelevance.com)
2+
# (c) 2005-2008 Relevance, Inc.. (http://thinkrelevance.com)
33
# Streamlined is freely distributable under the terms of an MIT-style license.
44
# For details, see http://streamlinedframework.org/
55

test/functional/streamlined_controller_functional_test.rb

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def setup
1919
it "delegated methods are not routable" do
2020
action_methods = PeopleController.action_methods.map(&:to_sym)
2121
(action_methods & Streamlined::Context::RequestContext::DELEGATES).size.should == 0
22-
(action_methods & Streamlined::Context::ControllerContext::DELEGATES).size.should == 0
2322
end
2423

2524
it "should render index" do

test/unit/streamlined/context/controller_context_test.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
describe "Streamlined::Context::ControllerContext" do
55

66
def setup
7-
@context = Streamlined::Context::ControllerContext.new
8-
@context.model_name = "String"
7+
@context = Streamlined::Context::ControllerContext.new(String)
98
end
109

1110
it "model ui" do
1211
assert_instance_of Streamlined::UI, @context.model_ui
13-
context2 = Streamlined::Context::ControllerContext.new
14-
context2.model_name = "Integer"
12+
context2 = Streamlined::Context::ControllerContext.new("Integer")
1513
assert_not_equal context2.model_ui, @context.model_ui, "every model class gets its own anonymous subclass for ui"
1614
end
1715

1816
it "model ui uses passed model class" do
1917
assert_equal String, @context.model_ui.model
18+
end
19+
20+
it "stringifies model class passed in, so callers can pass class or string" do
21+
assert_equal "String", @context.ui_model_name
2022
end
2123
end

test/unit/streamlined/controller/controller_test.rb

+13-25
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,24 @@ class FooController < ApplicationController
88
describe "Streamlined::Controller" do
99
include Streamlined::Controller::ClassMethods
1010

11-
# verify that exception is logged and rethrown
12-
it "initialize with streamlined variables" do
13-
o = Object.new
14-
o.extend Streamlined::Controller::InstanceMethods
15-
logger = flexmock("logger") do |mock|
16-
mock.should_receive(:info).once
17-
end
18-
flexmock(o, :streamlined_logger => logger)
19-
flexmock(Streamlined::Context::ControllerContext).should_receive(:new).and_raise(RuntimeError,"mocked!")
20-
assert_raise(RuntimeError) do
21-
o.send :initialize_streamlined_values
22-
end
11+
before do
12+
@clazz = Class.new
13+
@clazz.extend Streamlined::Controller::ClassMethods
2314
end
2415

25-
it "deprecation of helper overrides" do
26-
c = FooController
27-
c.acts_as_streamlined
28-
assert_nil c.send(:instance_variable_get, :@helper_overrides)
29-
assert_raises(ArgumentError) do
30-
c.acts_as_streamlined :helpers => ["NEW HELPER"]
31-
end
16+
it "initialize streamlined controller context" do
17+
@clazz.expects(:delegate_non_routable)
18+
@clazz.initialize_streamlined_controller_context("Foo")
19+
context = @clazz.streamlined_controller_context
20+
context.should.be.instance_of Streamlined::Context::ControllerContext
3221
end
3322

3423
it "streamlined model" do
35-
streamlined_model("Test")
36-
assert_equal "Test", model_name
37-
streamlined_model(stub(:name => "Tim"))
38-
assert_equal "Tim",
39-
model_name,
40-
"streamlined_model should extract name property"
24+
@clazz.expects(:delegate_non_routable)
25+
@clazz.initialize_streamlined_controller_context("Foo")
26+
@clazz.model_name.should == "Foo"
27+
@clazz.streamlined_ui("Bar")
28+
@clazz.model_name.should == "Bar"
4129
end
4230

4331
it "render filter" do

0 commit comments

Comments
 (0)