view_models 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.bundle/config +2 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +19 -0
- data/.yardoc/checksums +13 -0
- data/.yardoc/object_types +0 -0
- data/.yardoc/objects/root.dat +0 -0
- data/.yardoc/proxy_types +0 -0
- data/Appraisals +15 -0
- data/CHANGELOG +31 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +174 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +199 -0
- data/Rakefile +24 -0
- data/doc/ModulesInRenderHierarchy.html +186 -0
- data/doc/ModulesInRenderHierarchy/ClassMethods.html +188 -0
- data/doc/ViewModels.html +202 -0
- data/doc/ViewModels/Base.html +1342 -0
- data/doc/ViewModels/ContextExtractor.html +406 -0
- data/doc/ViewModels/Extensions.html +128 -0
- data/doc/ViewModels/Extensions/ModelReader.html +255 -0
- data/doc/ViewModels/Extensions/ModelReader/FilteredDelegationInstaller.html +908 -0
- data/doc/ViewModels/Extensions/ModelReader/Options.html +551 -0
- data/doc/ViewModels/Extensions/View.html +303 -0
- data/doc/ViewModels/Helpers.html +129 -0
- data/doc/ViewModels/Helpers/Mapping.html +562 -0
- data/doc/ViewModels/Helpers/Mapping/Collection.html +844 -0
- data/doc/ViewModels/Helpers/View.html +287 -0
- data/doc/ViewModels/PathStore.html +745 -0
- data/doc/ViewModels/RenderOptions.html +126 -0
- data/doc/ViewModels/RenderOptions/Base.html +1187 -0
- data/doc/ViewModels/RenderOptions/Partial.html +231 -0
- data/doc/ViewModels/RenderOptions/Template.html +231 -0
- data/doc/ViewModels/View.html +414 -0
- data/doc/ViewModelsGenerator.html +410 -0
- data/doc/_index.html +317 -0
- data/doc/class_list.html +53 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +328 -0
- data/doc/file.README.html +113 -0
- data/doc/file_list.html +55 -0
- data/doc/frames.html +28 -0
- data/doc/index.html +113 -0
- data/doc/js/app.js +214 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +628 -0
- data/doc/top-level-namespace.html +114 -0
- data/feature_support/testapp/app/controllers/heroes_controller.rb +6 -0
- data/feature_support/testapp/app/controllers/users_controller.rb +6 -0
- data/feature_support/testapp/app/helpers/application_helper.rb +8 -0
- data/feature_support/testapp/app/models/hero.rb +8 -0
- data/feature_support/testapp/app/models/user.rb +3 -0
- data/feature_support/testapp/app/view_models/hero.rb +8 -0
- data/feature_support/testapp/app/view_models/test.rb +8 -0
- data/feature_support/testapp/app/view_models/user.rb +14 -0
- data/feature_support/testapp/app/views/heroes/show.html.slim +6 -0
- data/feature_support/testapp/app/views/users/_box.html.slim +8 -0
- data/feature_support/testapp/app/views/users/show.html.slim +6 -0
- data/feature_support/testapp/config/application.rb +64 -0
- data/feature_support/testapp/config/cucumber.yml +8 -0
- data/feature_support/testapp/config/database.yml +28 -0
- data/feature_support/testapp/config/environments/cucumber.rb +11 -0
- data/feature_support/testapp/config/environments/test.rb +27 -0
- data/feature_support/testapp/config/routes.rb +4 -0
- data/feature_support/testapp/db/migrate/1_create_users.rb +13 -0
- data/feature_support/testapp/features/inheritance.feature +16 -0
- data/feature_support/testapp/features/step_definitions/testapp_steps.rb +16 -0
- data/feature_support/testapp/features/support/env.rb +13 -0
- data/feature_support/testapp/features/users.feature +15 -0
- data/feature_support/testapp/lib/tasks/cucumber.rake +31 -0
- data/feature_support/testapp/spec/factories/heroes.rb +7 -0
- data/feature_support/testapp/spec/factories/users.rb +7 -0
- data/features/rails_integration.feature +21 -0
- data/features/step_definitions/rails_steps.rb +112 -0
- data/features/support/env.rb +6 -0
- data/gemfiles/3.0.Gemfile +13 -0
- data/gemfiles/3.1.Gemfile +15 -0
- data/gemfiles/3.2.gemfile +15 -0
- data/lib/rails/generators/view_models/USAGE +6 -0
- data/lib/rails/generators/view_models/templates/views/_collection.html.erb +6 -0
- data/lib/{rails2/generators/view_models/templates/views/view_models/collection → rails/generators/view_models/templates/views}/_collection.html.haml +1 -2
- data/lib/rails/generators/view_models/templates/views/_collection.html.slim +6 -0
- data/lib/{rails2/generators/view_models/templates/views/_empty.html.haml → rails/generators/view_models/templates/views/_empty.html.erb} +0 -0
- data/lib/rails/generators/view_models/templates/views/_empty.html.haml +0 -0
- data/lib/rails/generators/view_models/templates/views/_empty.html.slim +0 -0
- data/lib/rails/generators/view_models/templates/views/_list.html.erb +6 -0
- data/lib/rails/generators/view_models/templates/views/_list.html.haml +4 -0
- data/lib/rails/generators/view_models/templates/views/_list.html.slim +5 -0
- data/lib/rails/generators/view_models/templates/views/_pagination.html.erb +18 -0
- data/lib/{rails2/generators/view_models/templates/views/view_models/collection → rails/generators/view_models/templates/views}/_pagination.html.haml +2 -2
- data/lib/rails/generators/view_models/templates/views/_pagination.html.slim +12 -0
- data/lib/rails/generators/view_models/templates/views/_table.html.erb +10 -0
- data/lib/{rails2/generators/view_models/templates/views/view_models/collection → rails/generators/view_models/templates/views}/_table.html.haml +0 -0
- data/lib/rails/generators/view_models/templates/views/_table.html.slim +5 -0
- data/lib/rails/generators/view_models/view_models_generator.rb +57 -0
- data/lib/view_models.rb +15 -5
- data/lib/{shared/lib/view_models → view_models}/base.rb +148 -37
- data/lib/{shared/lib/view_models → view_models}/context_extractor.rb +6 -2
- data/lib/{shared/lib/view_models → view_models}/extensions/model_reader.rb +27 -13
- data/lib/{rails2/lib → view_models}/extensions/view.rb +6 -1
- data/lib/view_models/helpers/collection.rb +113 -0
- data/lib/{shared/lib/view_models → view_models}/helpers/mapping.rb +12 -15
- data/lib/{rails2/lib → view_models}/helpers/view.rb +5 -1
- data/lib/{shared/lib/view_models → view_models}/path_store.rb +6 -1
- data/lib/{shared/lib/view_models → view_models}/render_options.rb +19 -4
- data/lib/view_models/version.rb +5 -0
- data/lib/{rails2/lib/view_models → view_models}/view.rb +7 -6
- data/spec/{rails2/lib → lib}/view_models/base_spec.rb +38 -5
- data/spec/{rails2/lib → lib/view_models}/extensions/model_reader_spec.rb +4 -4
- data/spec/{shared/lib → lib/view_models}/helpers/collection_spec.rb +21 -1
- data/spec/{shared/lib → lib/view_models}/helpers/mapping_spec.rb +6 -5
- data/spec/{rails2/lib → lib/view_models}/helpers/view_spec.rb +1 -1
- data/spec/lib/view_models/view_spec.rb +42 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/spec_helper_extensions.rb +13 -0
- data/view_models.gemspec +36 -0
- metadata +411 -142
- data/lib/init.rb +0 -1
- data/lib/padrino/README.textile +0 -3
- data/lib/padrino/init.rb +0 -10
- data/lib/padrino/lib/helpers/collection.rb +0 -27
- data/lib/padrino/lib/padrino/view_models.rb +0 -19
- data/lib/padrino/lib/view_models/base.rb +0 -65
- data/lib/rails2/README.textile +0 -3
- data/lib/rails2/TODO.textile +0 -5
- data/lib/rails2/generators/view_models/USAGE +0 -6
- data/lib/rails2/generators/view_models/templates/README +0 -1
- data/lib/rails2/generators/view_models/templates/spec/view_model_spec.rb +0 -7
- data/lib/rails2/generators/view_models/templates/view_models/view_model.rb +0 -5
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_collection.html.erb +0 -1
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_collection.text.erb +0 -6
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_list.html.erb +0 -1
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_list.text.erb +0 -6
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_pagination.text.erb +0 -3
- data/lib/rails2/generators/view_models/templates/views/view_models/collection/_table.text.erb +0 -10
- data/lib/rails2/generators/view_models/view_models_generator.rb +0 -47
- data/lib/rails2/init.rb +0 -18
- data/lib/rails2/lib/experimental/README.textile +0 -32
- data/lib/rails2/lib/experimental/modules_in_render_hierarchy.rb +0 -21
- data/lib/rails2/lib/helpers/collection.rb +0 -27
- data/lib/rails2/lib/view_models.rb +0 -1
- data/lib/rails2/lib/view_models/base.rb +0 -99
- data/lib/shared/README.textile +0 -3
- data/lib/shared/init.rb +0 -11
- data/lib/shared/lib/view_models.rb +0 -4
- data/lib/shared/lib/view_models/extensions/active_record.rb +0 -27
- data/lib/shared/lib/view_models/helpers/collection.rb +0 -110
- data/spec/padrino/integration/integration_spec.rb +0 -287
- data/spec/padrino/lib/helpers/collection_spec.rb +0 -30
- data/spec/rails2/integration/integration_spec.rb +0 -279
- data/spec/rails2/lib/extensions/active_record_spec.rb +0 -31
- data/spec/rails2/lib/helpers/collection_spec.rb +0 -30
- data/spec/rails2/lib/view_models/view_spec.rb +0 -12
data/lib/shared/README.textile
DELETED
data/lib/shared/init.rb
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/extensions/active_record')
|
|
2
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/extensions/model_reader')
|
|
3
|
-
|
|
4
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models')
|
|
5
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/path_store')
|
|
6
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/render_options')
|
|
7
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/context_extractor')
|
|
8
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/base')
|
|
9
|
-
|
|
10
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/helpers/mapping')
|
|
11
|
-
require File.join(File.dirname(__FILE__), '/lib/view_models/helpers/collection')
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Makes certain AR Model methods available to the view model.
|
|
2
|
-
#
|
|
3
|
-
# Useful when the model is an AR Model.
|
|
4
|
-
#
|
|
5
|
-
# TODO Split into id delegations and AR/AC specific stuff.
|
|
6
|
-
#
|
|
7
|
-
module ViewModels
|
|
8
|
-
module Extensions
|
|
9
|
-
module ActiveRecord
|
|
10
|
-
|
|
11
|
-
# id and param are simply delegated to the model.
|
|
12
|
-
#
|
|
13
|
-
# This makes it possible to use the view_model
|
|
14
|
-
# for e.g. url generation:
|
|
15
|
-
# * edit_user_path(view_model)
|
|
16
|
-
#
|
|
17
|
-
delegate :id, :to_param, :to => :model
|
|
18
|
-
|
|
19
|
-
# Delegate to the action controller record identifier.
|
|
20
|
-
#
|
|
21
|
-
def dom_id
|
|
22
|
-
ActionController::RecordIdentifier.dom_id model
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
module ViewModels
|
|
2
|
-
module Helpers
|
|
3
|
-
module Mapping
|
|
4
|
-
|
|
5
|
-
# Construct a view_model for a collection.
|
|
6
|
-
#
|
|
7
|
-
# TODO Think about moving it into view_model_for, or renaming it view_models_for.
|
|
8
|
-
#
|
|
9
|
-
def collection_view_model_for array_or_pagination, context = self
|
|
10
|
-
Collection.new array_or_pagination, context
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
# The Collection view_model helper has the purpose of presenting presentable collections.
|
|
14
|
-
# * Render as list
|
|
15
|
-
# * Render as table
|
|
16
|
-
# * Render as collection
|
|
17
|
-
# * Render a pagination
|
|
18
|
-
#
|
|
19
|
-
class Collection
|
|
20
|
-
|
|
21
|
-
# Delegate collection relevant methods to the collection.
|
|
22
|
-
#
|
|
23
|
-
self.delegate *[Enumerable.instance_methods, :length, :size, :empty?, :each, :exit, { :to => :@collection }].flatten
|
|
24
|
-
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
def initialize collection, context
|
|
28
|
-
@collection, @context = collection, context
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Renders a list (in the broadest sense of the word).
|
|
32
|
-
#
|
|
33
|
-
# Options:
|
|
34
|
-
# collection => collection to iterate over
|
|
35
|
-
# context => context to render in
|
|
36
|
-
# template_name => template to render for each model element
|
|
37
|
-
# separator => separator between each element
|
|
38
|
-
# By default, uses:
|
|
39
|
-
# * The collection of the collection view_model to iterate over.
|
|
40
|
-
# * The original context given to the collection view_model to render in.
|
|
41
|
-
# * Uses :list_item as the default element template.
|
|
42
|
-
# * Uses a nil separator in html.
|
|
43
|
-
#
|
|
44
|
-
def list options = {}
|
|
45
|
-
default_options = { :collection => @collection, :template_name => :list_item, :separator => nil }
|
|
46
|
-
|
|
47
|
-
render_partial :list, default_options.merge(options)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# Renders a collection.
|
|
51
|
-
#
|
|
52
|
-
# Note: The only difference between a list and a collection is the enclosing
|
|
53
|
-
# list type. While list uses ol, the collection uses ul.
|
|
54
|
-
#
|
|
55
|
-
# Options:
|
|
56
|
-
# collection => collection to iterate over
|
|
57
|
-
# context => context to render in
|
|
58
|
-
# template_name => template to render for each model element
|
|
59
|
-
# separator => separator between each element
|
|
60
|
-
# By default, uses:
|
|
61
|
-
# * The collection of the collection view_model to iterate over.
|
|
62
|
-
# * Uses :collection_item as the default element template.
|
|
63
|
-
# * Uses a nil separator.
|
|
64
|
-
#
|
|
65
|
-
def collection options = {}
|
|
66
|
-
default_options = { :collection => @collection, :template_name => :collection_item, :separator => nil }
|
|
67
|
-
|
|
68
|
-
render_partial :collection, default_options.merge(options)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# Renders a table.
|
|
72
|
-
#
|
|
73
|
-
# Note: Each item represents a table row.
|
|
74
|
-
#
|
|
75
|
-
# Options:
|
|
76
|
-
# collection => collection to iterate over
|
|
77
|
-
# context => context to render in
|
|
78
|
-
# template_name => template to render for each model element
|
|
79
|
-
# separator => separator between each element
|
|
80
|
-
# By default, uses:
|
|
81
|
-
# * The collection of the collection view_model to iterate over.
|
|
82
|
-
# * Uses :table_row as the default element template.
|
|
83
|
-
# * Uses a nil separator.
|
|
84
|
-
#
|
|
85
|
-
def table options = {}
|
|
86
|
-
default_options = { :collection => @collection, :template_name => :table_row, :separator => nil }
|
|
87
|
-
|
|
88
|
-
render_partial :table, default_options.merge(options)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# Renders a pagination.
|
|
92
|
-
#
|
|
93
|
-
# Options:
|
|
94
|
-
# collection => collection to iterate over
|
|
95
|
-
# context => context to render in
|
|
96
|
-
# separator => separator between pages
|
|
97
|
-
# By default, uses:
|
|
98
|
-
# * The collection of the collection view_model to iterate over.
|
|
99
|
-
# * Uses | as separator.
|
|
100
|
-
#
|
|
101
|
-
def pagination options = {}
|
|
102
|
-
default_options = { :collection => @collection, :separator => '|' }
|
|
103
|
-
|
|
104
|
-
render_partial :pagination, default_options.merge(options)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
end
|
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), '../spec_helper')
|
|
2
|
-
|
|
3
|
-
require File.join(File.dirname(__FILE__), 'models/subclass')
|
|
4
|
-
require File.join(File.dirname(__FILE__), 'models/sub_subclass')
|
|
5
|
-
|
|
6
|
-
require File.join(File.dirname(__FILE__), 'view_models/project')
|
|
7
|
-
require File.join(File.dirname(__FILE__), 'view_models/subclass')
|
|
8
|
-
require File.join(File.dirname(__FILE__), 'view_models/sub_subclass')
|
|
9
|
-
require File.join(File.dirname(__FILE__), 'view_models/module_for_rendering')
|
|
10
|
-
|
|
11
|
-
class TestApp < Padrino::Application
|
|
12
|
-
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
TestApp.helpers ViewModels::Helpers::Mapping
|
|
16
|
-
|
|
17
|
-
describe 'Integration' do
|
|
18
|
-
|
|
19
|
-
before(:each) do
|
|
20
|
-
begin
|
|
21
|
-
# @controller = TestController.new
|
|
22
|
-
# @controller.class.view_paths = ['rails/spec/integration/views']
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
# @request = ActionController::TestRequest.new
|
|
26
|
-
# @response = ActionController::TestResponse.new
|
|
27
|
-
# @controller.request = @request
|
|
28
|
-
# @controller.response = @response
|
|
29
|
-
#
|
|
30
|
-
# # TODO Make separate contexts, one where the controller has rendered, one where it has not.
|
|
31
|
-
# #
|
|
32
|
-
# # Let the Controller generate a view instance.
|
|
33
|
-
# #
|
|
34
|
-
# # @controller.process @request, @response
|
|
35
|
-
#
|
|
36
|
-
# @view = ActionView::Base.new @controller.class.view_paths, {}, @controller
|
|
37
|
-
|
|
38
|
-
@app = stub :app
|
|
39
|
-
TestApp.new(@app) do |some_app|
|
|
40
|
-
@context = some_app
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
@logger = stub :logger, :null_object => true
|
|
44
|
-
Thread.current[:padrino_logger] = @logger
|
|
45
|
-
|
|
46
|
-
@model = SubSubclass.new
|
|
47
|
-
@model.id = :some_id
|
|
48
|
-
@view_model = ViewModels::SubSubclass.new @model, @context
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
before(:all) { puts "\n#{self.send(:description_args)[0]}:" }
|
|
53
|
-
|
|
54
|
-
# context 'experimental inclusion of module in render hierarchy' do
|
|
55
|
-
# before(:each) do
|
|
56
|
-
# ViewModels::Base.send :include, ModulesInRenderHierarchy
|
|
57
|
-
# @view_model.class.send :include, ModuleForRendering
|
|
58
|
-
# end
|
|
59
|
-
# describe 'render_as' do
|
|
60
|
-
# it 'should description' do
|
|
61
|
-
# @view_model.render_as(:not_found_in_sub_subclass_but_in_module).should == '_not_found_in_sub_subclass_but_in_module.erb'
|
|
62
|
-
# end
|
|
63
|
-
# end
|
|
64
|
-
# end
|
|
65
|
-
|
|
66
|
-
# TODO Not ActiveRecord Extensions.
|
|
67
|
-
#
|
|
68
|
-
describe 'ActiveRecord Extensions' do
|
|
69
|
-
before(:each) do
|
|
70
|
-
@view_model.extend ViewModels::Extensions::ActiveRecord
|
|
71
|
-
end
|
|
72
|
-
it 'should delegate the id' do
|
|
73
|
-
@view_model.id.should == :some_id
|
|
74
|
-
end
|
|
75
|
-
# it 'should delegate the id' do
|
|
76
|
-
# @view_model.dom_id.should == 'sub_subclass_some_id'
|
|
77
|
-
# end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
describe 'view_model_for' do
|
|
81
|
-
context 'view model' do
|
|
82
|
-
it 'should be available' do
|
|
83
|
-
lambda { @view_model.view_model_for @model }.should_not raise_error
|
|
84
|
-
end
|
|
85
|
-
it 'should return the right one' do
|
|
86
|
-
@view_model.view_model_for(@model).should be_kind_of(ViewModels::SubSubclass)
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
describe 'view_model_class_for' do
|
|
91
|
-
context 'view model' do
|
|
92
|
-
it 'should be available' do
|
|
93
|
-
lambda { @view_model.view_model_class_for @model }.should_not raise_error
|
|
94
|
-
end
|
|
95
|
-
it 'should return the right class' do
|
|
96
|
-
@view_model.view_model_class_for(@model).should == ViewModels::SubSubclass
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# describe 'capture in view model method' do
|
|
102
|
-
# context 'template' do
|
|
103
|
-
# it 'should capture the content of the block' do
|
|
104
|
-
# @view_model.render_as(:capture_in_template).should == 'Capturing: A Pirate!'
|
|
105
|
-
# end
|
|
106
|
-
# end
|
|
107
|
-
# context 'in view model' do
|
|
108
|
-
# it 'should capture the content of the block' do
|
|
109
|
-
# @view_model.render_as(:capture_in_view_model).should == 'Capturing: A Pirate!'
|
|
110
|
-
# end
|
|
111
|
-
# end
|
|
112
|
-
# end
|
|
113
|
-
|
|
114
|
-
# TODO Write similar.
|
|
115
|
-
# describe 'app context' do
|
|
116
|
-
# it 'should work' do
|
|
117
|
-
# controller = ActionController::Base.new
|
|
118
|
-
#
|
|
119
|
-
# lambda {
|
|
120
|
-
# ViewModels::SubSubclass.new @model, controller
|
|
121
|
-
# }.should_not raise_error
|
|
122
|
-
# end
|
|
123
|
-
# end
|
|
124
|
-
|
|
125
|
-
# TODO Write anew.
|
|
126
|
-
# describe 'view_model_for inclusion in view' do
|
|
127
|
-
# it 'should be included' do
|
|
128
|
-
# view = ActionView::Base.new
|
|
129
|
-
#
|
|
130
|
-
# lambda {
|
|
131
|
-
# view.view_model_for @model
|
|
132
|
-
# }.should_not raise_error
|
|
133
|
-
# end
|
|
134
|
-
# end
|
|
135
|
-
|
|
136
|
-
describe 'collection rendering' do
|
|
137
|
-
context 'default format' do
|
|
138
|
-
it 'should render a html list' do
|
|
139
|
-
@view_model.render_as(:list_example).should == "\n\n<ol class=\"collection\"><li>_list_item.html.erb</li><li>_list_item.html.erb</li></ol>"
|
|
140
|
-
end
|
|
141
|
-
it 'should render a html collection' do
|
|
142
|
-
@view_model.render_as(:collection_example).should == "\n\n<ul class=\"collection\"><li>_collection_item.html.erb</li><li>_collection_item.html.erb</li></ul>"
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
context 'format html' do
|
|
146
|
-
it 'should render a html list' do
|
|
147
|
-
@view_model.render_as(:list_example, :format => :html).should == "\n\n<ol class=\"collection\"><li>_list_item.html.erb</li><li>_list_item.html.erb</li></ol>"
|
|
148
|
-
end
|
|
149
|
-
it 'should render a html collection' do
|
|
150
|
-
@view_model.render_as(:collection_example, :format => :html).should == "\n\n<ul class=\"collection\"><li>_collection_item.html.erb</li><li>_collection_item.html.erb</li></ul>"
|
|
151
|
-
end
|
|
152
|
-
end
|
|
153
|
-
context 'format text' do
|
|
154
|
-
it 'should render a text list' do
|
|
155
|
-
@view_model.render_as(:list_example, :format => :text).should == "\n\n_list_item.text.erb\\n_list_item.text.erb"
|
|
156
|
-
end
|
|
157
|
-
it 'should render a text collection' do
|
|
158
|
-
@view_model.render_as(:collection_example, :format => :text).should == "\n\n_collection_item.text.erb_collection_item.text.erb"
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
describe 'model attributes' do
|
|
164
|
-
it 'should pass through unfiltered attributes' do
|
|
165
|
-
@view_model.some_untouched_attribute.should == :some_value
|
|
166
|
-
end
|
|
167
|
-
it 'should filter some attributes' do
|
|
168
|
-
@view_model.some_filtered_attribute.should == '%3Cscript%3Efilter+me%3C%2Fscript%3E'
|
|
169
|
-
end
|
|
170
|
-
it 'should filter some attributes multiple times' do
|
|
171
|
-
@view_model.some_doubly_doubled_attribute.should == 'blahblahblahblah'
|
|
172
|
-
end
|
|
173
|
-
it 'should filter some attributes multiple times correctly' do
|
|
174
|
-
@view_model.some_mangled_attribute.should == 'DCBDCB'
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
describe 'app_method' do
|
|
179
|
-
it 'should delegate to the context' do
|
|
180
|
-
@view_model.logger.should == @logger
|
|
181
|
-
end
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
describe "render_template" do
|
|
185
|
-
it "should render the right template" do
|
|
186
|
-
@view_model.render_template(:show).should == 'show.html.erb'
|
|
187
|
-
end
|
|
188
|
-
it "should render the right template with format" do
|
|
189
|
-
@view_model.render_template(:show, :format => :text).should == 'show.text.erb'
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
describe 'render_as' do
|
|
194
|
-
describe 'render_the alias' do
|
|
195
|
-
it 'should also render' do
|
|
196
|
-
@view_model.render_the(:part_that_is_dependent_on_the_view_model).should == '_part_that_is_dependent_on_the_view_model.erb'
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
describe "explicit partial rendering" do
|
|
200
|
-
it "should render the right partial" do
|
|
201
|
-
# If one wants explicit template rendering, he needs to work more.
|
|
202
|
-
# Let's be opinionated here :)
|
|
203
|
-
#
|
|
204
|
-
@view_model.render_as(:partial => 'view_models/sub_subclass/inner', :format => :nesting).should == '_inner.nesting.erb'
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
describe "nesting" do
|
|
208
|
-
it "should render the right nested template, with an explicitly defined format (see template)" do
|
|
209
|
-
@view_model.render_as(:outer, :format => :explicit).should == '_inner.also_explicit.erb'
|
|
210
|
-
end
|
|
211
|
-
it "should render the right nested template, respecting the already defined format" do
|
|
212
|
-
@view_model.render_as(:outer, :format => :nesting).should == '_inner.nesting.erb'
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
describe 'template inheritance' do
|
|
216
|
-
it 'should raise ViewModels::MissingTemplateError if template is not found' do
|
|
217
|
-
lambda { @view_model.render_as(:this_template_does_not_exist_at_allllll) }.should raise_error(ViewModels::MissingTemplateError, "No template '_this_template_does_not_exist_at_allllll' with default format found.")
|
|
218
|
-
end
|
|
219
|
-
it 'should raise ViewModels::MissingTemplateError if template is not found, with specific path' do
|
|
220
|
-
lambda { @view_model.render_as(:partial => 'view_models/sub_subclass/this_template_does_not_exist_at_allllll') }.should raise_error(ViewModels::MissingTemplateError, "No template 'view_models/sub_subclass/_this_template_does_not_exist_at_allllll' with default format found.")
|
|
221
|
-
end
|
|
222
|
-
it 'should raise ViewModels::MissingTemplateError if template is not found, with format' do
|
|
223
|
-
lambda { @view_model.render_as(:this_template_does_not_exist_at_allllll, :format => :gaga) }.should raise_error(ViewModels::MissingTemplateError, "No template '_this_template_does_not_exist_at_allllll' with format gaga found.")
|
|
224
|
-
end
|
|
225
|
-
it "should use its own template" do
|
|
226
|
-
@view_model.render_as(:exists).should == '_exists.html.erb' # The default
|
|
227
|
-
end
|
|
228
|
-
it "should use the subclass' template" do
|
|
229
|
-
@view_model.render_as(:no_sub_subclass).should == '_no_sub_subclass.erb'
|
|
230
|
-
end
|
|
231
|
-
end
|
|
232
|
-
describe 'format' do
|
|
233
|
-
it 'should render html' do
|
|
234
|
-
@view_model.render_as(:exists, :format => nil).should == '_exists.html.erb' # the default
|
|
235
|
-
end
|
|
236
|
-
it 'should render erb' do
|
|
237
|
-
@view_model.render_as(:exists, :format => '').should == '_exists.erb'
|
|
238
|
-
end
|
|
239
|
-
it 'should render text' do
|
|
240
|
-
@view_model.render_as(:exists, :format => :text).should == '_exists.text.erb'
|
|
241
|
-
end
|
|
242
|
-
it 'should render html' do
|
|
243
|
-
@view_model.render_as(:exists, :format => :html).should == '_exists.html.erb'
|
|
244
|
-
end
|
|
245
|
-
end
|
|
246
|
-
describe 'locals' do
|
|
247
|
-
it 'should render html' do
|
|
248
|
-
@view_model.render_as(:exists, :locals => { :local_name => :some_local }).should == '_exists.html.erb with some_local'
|
|
249
|
-
end
|
|
250
|
-
it 'should render html' do
|
|
251
|
-
@view_model.render_as(:exists, :format => nil, :locals => { :local_name => :some_local }).should == '_exists.html.erb with some_local'
|
|
252
|
-
end
|
|
253
|
-
it 'should render text' do
|
|
254
|
-
@view_model.render_as(:exists, :format => '', :locals => { :local_name => :some_local }).should == '_exists.erb with some_local'
|
|
255
|
-
end
|
|
256
|
-
it 'should render text' do
|
|
257
|
-
@view_model.render_as(:exists, :format => :text, :locals => { :local_name => :some_local }).should == '_exists.text.erb with some_local'
|
|
258
|
-
end
|
|
259
|
-
it 'should render html' do
|
|
260
|
-
@view_model.render_as(:exists, :format => :html, :locals => { :local_name => :some_local }).should == '_exists.html.erb with some_local'
|
|
261
|
-
end
|
|
262
|
-
end
|
|
263
|
-
describe 'memoizing' do
|
|
264
|
-
it 'should memoize and not generate always a new path' do
|
|
265
|
-
@view_model.class.should_receive(:generate_template_path_from).once.and_return "view_models/sub_subclass/_not_found_in_sub_subclass"
|
|
266
|
-
|
|
267
|
-
@view_model.render_as :not_found_in_sub_subclass
|
|
268
|
-
@view_model.render_as :not_found_in_sub_subclass
|
|
269
|
-
@view_model.render_as :not_found_in_sub_subclass
|
|
270
|
-
@view_model.render_as :not_found_in_sub_subclass
|
|
271
|
-
@view_model.render_as :not_found_in_sub_subclass
|
|
272
|
-
end
|
|
273
|
-
it 'should render the right one' do
|
|
274
|
-
@view_model.render_as :exists_in_both
|
|
275
|
-
@view_model.render_as(:exists_in_both).should == 'in sub subclass'
|
|
276
|
-
|
|
277
|
-
other_view_model = ViewModels::Subclass.new Subclass.new, @context
|
|
278
|
-
other_view_model.render_as :exists_in_both
|
|
279
|
-
other_view_model.render_as(:exists_in_both).should == 'in subclass'
|
|
280
|
-
|
|
281
|
-
@view_model.render_as :exists_in_both
|
|
282
|
-
@view_model.render_as(:exists_in_both).should == 'in sub subclass'
|
|
283
|
-
end
|
|
284
|
-
end
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
end
|