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
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
module ViewModels
|
|
2
|
+
|
|
3
|
+
# Helpers you can include in just about anything to get convenient access to view model functionality
|
|
4
|
+
#
|
|
5
|
+
module Helpers
|
|
6
|
+
|
|
7
|
+
# Mapping helpers install collection_view_model_for and view_model_for, which you can use with instances
|
|
8
|
+
# to conveniently instantiate a view model for it
|
|
9
|
+
#
|
|
10
|
+
module Mapping
|
|
11
|
+
|
|
12
|
+
# Construct a view_model for a collection.
|
|
13
|
+
#
|
|
14
|
+
# @todo Think about moving it into view_model_for, or renaming it view_models_for.
|
|
15
|
+
#
|
|
16
|
+
def collection_view_model_for array_or_pagination, context = self
|
|
17
|
+
Collection.new array_or_pagination, context
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The Collection view_model helper has the purpose of presenting presentable collections.
|
|
21
|
+
# * Render as list
|
|
22
|
+
# * Render as table
|
|
23
|
+
# * Render as collection
|
|
24
|
+
# * Render a pagination
|
|
25
|
+
#
|
|
26
|
+
class Collection
|
|
27
|
+
|
|
28
|
+
# Delegate collection relevant methods to the collection.
|
|
29
|
+
# @todo Why is as_json not yet loaded in Enumerable.instance_methods
|
|
30
|
+
# when this file is loaded in the spec, require active_support is installed before view_models are loaded
|
|
31
|
+
# Load Order ? Rails Blagic?
|
|
32
|
+
#
|
|
33
|
+
self.delegate *[Enumerable.instance_methods, :length, :size, :empty?, :each, :exit, :as_json, { :to => :@collection }].flatten
|
|
34
|
+
|
|
35
|
+
#
|
|
36
|
+
#
|
|
37
|
+
def initialize collection, context
|
|
38
|
+
@collection, @context = collection, context
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Renders a list (in the broadest sense of the word).
|
|
42
|
+
#
|
|
43
|
+
# @param [Hash] options The options for the list
|
|
44
|
+
# @option options :collection the collection to iterate over (default: The collection of the collection view_model to iterate over)
|
|
45
|
+
# @option options :context context to render in (default: The original context given to the collection view_model to render in)
|
|
46
|
+
# @option options :template_name the template to render for each model element (default: list_item as the default element template)
|
|
47
|
+
# @option options :separator the separator between each element (default: nil separator in html)
|
|
48
|
+
#
|
|
49
|
+
def list options = {}
|
|
50
|
+
render_partial :list, template_locals(:list_item, options)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Renders a collection.
|
|
54
|
+
#
|
|
55
|
+
# @note The only difference between a list and a collection is the enclosing list type. While list uses ol, the collection uses ul.
|
|
56
|
+
#
|
|
57
|
+
# @param [Hash] options The options for the collection
|
|
58
|
+
# @option options :collection the collection to iterate over (default: The collection of the collection view_model to iterate over)
|
|
59
|
+
# @option options :context the context to render in (default: The original context given to the collection view_model to render in)
|
|
60
|
+
# @option options :template_name the template to render for each model element (default: Uses :collection_item as the default element template)
|
|
61
|
+
# @option options :separator => separator between each element (default: nil separator in html)
|
|
62
|
+
#
|
|
63
|
+
def collection options = {}
|
|
64
|
+
render_partial :collection, template_locals(:collection_item, options)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Renders a table.
|
|
68
|
+
#
|
|
69
|
+
# Note: Each item represents a table row.
|
|
70
|
+
#
|
|
71
|
+
# @param [Hash] options The options for the table
|
|
72
|
+
# @option options :collection the collection to iterate over (default: The collection of the collection view_model to iterate over)
|
|
73
|
+
# @option options :context the context to render in (default: The original context given to the collection view_model to render in)
|
|
74
|
+
# @option options :template_name the template to render for each model element (default: Uses :table_row as the default element template)
|
|
75
|
+
# @option options :separator => separator between each element (default: nil separator in html)
|
|
76
|
+
#
|
|
77
|
+
def table options = {}
|
|
78
|
+
render_partial :table, template_locals(:table_row, options)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Renders a pagination.
|
|
82
|
+
#
|
|
83
|
+
# @param [Hash] options The options for the collection
|
|
84
|
+
# @option options :collection the collection to iterate over (default: The collection of the collection view_model to iterate over)
|
|
85
|
+
# @option options :context the context to render in (default: The original context given to the collection view_model to render in)
|
|
86
|
+
# @option options :template_name the template to render for each model element (default: Uses :collection_item as the default element template)
|
|
87
|
+
# @option options :separator => separator between each element (default: | separator in html)
|
|
88
|
+
#
|
|
89
|
+
def pagination options = {}
|
|
90
|
+
render_partial :pagination, template_locals(:pagination, {:separator => '|'}.merge(options))
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Helper method that renders a partial in the context of the context instance.
|
|
96
|
+
#
|
|
97
|
+
# @note If the collection view_model helper has been instantiated in the context of a controller, render will be called in the controller.
|
|
98
|
+
#
|
|
99
|
+
def render_partial name, locals
|
|
100
|
+
@context.instance_eval { render :partial => "view_models/collection/#{name}", :locals => locals }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Helper method that constructs locals for render_partial
|
|
104
|
+
#
|
|
105
|
+
def template_locals template_name, supersed_options
|
|
106
|
+
{ :collection => @collection, :template_name => template_name, :separator => nil }.merge supersed_options
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -2,16 +2,20 @@ module ViewModels
|
|
|
2
2
|
module Helpers
|
|
3
3
|
module Mapping
|
|
4
4
|
|
|
5
|
+
# module attribute used for specific mapping
|
|
6
|
+
#
|
|
5
7
|
mattr_accessor :specific_view_model_mapping
|
|
6
8
|
self.specific_view_model_mapping = {}
|
|
7
9
|
|
|
10
|
+
# module attribute for the default view models prefix
|
|
11
|
+
#
|
|
12
|
+
mattr_accessor :default_prefix
|
|
13
|
+
self.default_prefix = 'ViewModels::'
|
|
14
|
+
|
|
8
15
|
# Create a new view_model instance for the given model instance
|
|
9
16
|
# with the given arguments.
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# Padrino: context is an app instance.
|
|
14
|
-
# Rails 2: context is either a view instance or a controller instance.
|
|
17
|
+
# @raise [ArgumentError] if the view model class doesn't support 2 arguments.
|
|
18
|
+
# @note context is either a view instance or a controller instance.
|
|
15
19
|
#
|
|
16
20
|
def view_model_for model, context = self
|
|
17
21
|
view_model_class_for(model).new model, context
|
|
@@ -19,12 +23,7 @@ module ViewModels
|
|
|
19
23
|
|
|
20
24
|
# Get the view_model class for the given model instance.
|
|
21
25
|
#
|
|
22
|
-
#
|
|
23
|
-
# (As returned by default_view_model_class_for)
|
|
24
|
-
# Override specific_mapping if you'd like to install your own.
|
|
25
|
-
#
|
|
26
|
-
# OR: Override default_view_model_class_for(model) if
|
|
27
|
-
# you'd like to change the default.
|
|
26
|
+
# @note ViewModels are usually of class ViewModels::<ModelClassName>. (As returned by default_view_model_class_for). Override specific_mapping if you'd like to install your own. Or Override default_view_model_class_for(model) if you'd like to change the default.
|
|
28
27
|
#
|
|
29
28
|
def view_model_class_for model
|
|
30
29
|
specific_view_model_class_for(model) || default_view_model_class_for(model)
|
|
@@ -38,10 +37,8 @@ module ViewModels
|
|
|
38
37
|
# Override this method if you'd like to change the _default_
|
|
39
38
|
# model-to-view_model class mapping.
|
|
40
39
|
#
|
|
41
|
-
#
|
|
40
|
+
# @raise [NameError] if a corresponding ViewModels constant cannot be loaded.
|
|
42
41
|
#
|
|
43
|
-
mattr_accessor :default_prefix
|
|
44
|
-
self.default_prefix = 'ViewModels::'
|
|
45
42
|
def default_view_model_class_for model
|
|
46
43
|
(default_prefix + model.class.name).constantize
|
|
47
44
|
end
|
|
@@ -51,7 +48,7 @@ module ViewModels
|
|
|
51
48
|
# Override this method, if you want to return a specific
|
|
52
49
|
# view model class for the given model.
|
|
53
50
|
#
|
|
54
|
-
#
|
|
51
|
+
# @raise [NameError] if a corresponding ViewModels constant cannot be loaded.
|
|
55
52
|
#
|
|
56
53
|
def specific_view_model_class_for model
|
|
57
54
|
specific_view_model_mapping[model.class]
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
module ViewModels
|
|
2
2
|
module Helpers
|
|
3
|
+
|
|
3
4
|
# Module for conveniently including common view_helpers into a view_model
|
|
4
5
|
#
|
|
5
6
|
module View
|
|
6
7
|
|
|
7
|
-
# Include hook
|
|
8
|
+
# Include hook for the view
|
|
8
9
|
#
|
|
9
10
|
def self.included view_model
|
|
10
11
|
view_model.send :include, *all_view_helpers
|
|
11
12
|
end
|
|
12
13
|
|
|
14
|
+
# The common view helpers
|
|
15
|
+
# @return [Array] an array of common view helpers
|
|
16
|
+
#
|
|
13
17
|
def self.all_view_helpers
|
|
14
18
|
[
|
|
15
19
|
ActionView::Helpers,
|
|
@@ -8,8 +8,13 @@ module ViewModels
|
|
|
8
8
|
#
|
|
9
9
|
class PathStore
|
|
10
10
|
|
|
11
|
+
# The view model class attribute
|
|
12
|
+
#
|
|
11
13
|
attr_reader :view_model_class
|
|
12
14
|
|
|
15
|
+
# Initialize the path store
|
|
16
|
+
# @param [ViewModel] view_model_class The view model class
|
|
17
|
+
#
|
|
13
18
|
def initialize view_model_class
|
|
14
19
|
@view_model_class = view_model_class
|
|
15
20
|
@name_path_mapping = {}
|
|
@@ -31,7 +36,7 @@ module ViewModels
|
|
|
31
36
|
|
|
32
37
|
# Prepare the key for the next storing procedure.
|
|
33
38
|
#
|
|
34
|
-
#
|
|
39
|
+
# @note If this is nil, the store will not save the path.
|
|
35
40
|
#
|
|
36
41
|
def prepare key
|
|
37
42
|
@key = key
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
module ViewModels
|
|
2
2
|
|
|
3
3
|
# Container object for render options.
|
|
4
|
-
#
|
|
5
|
-
# TODO Make a rails exclusive.
|
|
6
4
|
#
|
|
7
5
|
module RenderOptions
|
|
8
6
|
|
|
@@ -10,8 +8,15 @@ module ViewModels
|
|
|
10
8
|
#
|
|
11
9
|
class Base
|
|
12
10
|
|
|
11
|
+
# The different attributes used to generate render options
|
|
12
|
+
#
|
|
13
13
|
attr_accessor :path, :name, :prefix, :file, :view_model, :format
|
|
14
14
|
|
|
15
|
+
# Initialize Render Options
|
|
16
|
+
# @param [String] prefix The prefix for rendering
|
|
17
|
+
# @param [String] name The template name
|
|
18
|
+
# @param [Hash] options The render options as a hash
|
|
19
|
+
#
|
|
15
20
|
def initialize prefix, name, options
|
|
16
21
|
@prefix = prefix
|
|
17
22
|
@options = options
|
|
@@ -24,16 +29,25 @@ module ViewModels
|
|
|
24
29
|
def error_message
|
|
25
30
|
"'#{error_path}#{name}' with #{error_format}"
|
|
26
31
|
end
|
|
32
|
+
|
|
33
|
+
# The error path, always returns a string
|
|
34
|
+
# @return [String] The error path
|
|
35
|
+
#
|
|
27
36
|
def error_path
|
|
28
37
|
path = self.path
|
|
29
38
|
path ? "#{path}/" : ""
|
|
30
39
|
end
|
|
40
|
+
|
|
41
|
+
# The format when trying to render
|
|
42
|
+
# @return [String] the render format
|
|
43
|
+
#
|
|
31
44
|
def error_format
|
|
32
45
|
format = self.format
|
|
33
46
|
format ? "format #{format}" : "default format"
|
|
34
47
|
end
|
|
35
48
|
|
|
36
49
|
# Used when rendering.
|
|
50
|
+
# @return [Hash] The render options
|
|
37
51
|
#
|
|
38
52
|
def to_render_options
|
|
39
53
|
@options[:locals] ||= {}
|
|
@@ -41,7 +55,8 @@ module ViewModels
|
|
|
41
55
|
@options.reverse_merge :file => file
|
|
42
56
|
end
|
|
43
57
|
|
|
44
|
-
#
|
|
58
|
+
# @todo Rails specific.
|
|
59
|
+
# @param [ActionView] view The view to render
|
|
45
60
|
#
|
|
46
61
|
def format! view
|
|
47
62
|
view.template_format = @format if @format
|
|
@@ -55,7 +70,7 @@ module ViewModels
|
|
|
55
70
|
|
|
56
71
|
private
|
|
57
72
|
|
|
58
|
-
#
|
|
73
|
+
# @todo rewrite
|
|
59
74
|
#
|
|
60
75
|
def template_name= template_name
|
|
61
76
|
template_name.to_s.include?('/') ? specific_path(template_name) : incomplete_path(template_name)
|
|
@@ -5,7 +5,8 @@ module ViewModels
|
|
|
5
5
|
|
|
6
6
|
# Shut up, opinionated funkers.
|
|
7
7
|
#
|
|
8
|
-
alias singleton_class metaclass unless instance_methods.include?('singleton_class')
|
|
8
|
+
alias singleton_class metaclass unless instance_methods.include?(:singleton_class) || instance_methods.include?('singleton_class')
|
|
9
|
+
|
|
9
10
|
|
|
10
11
|
# Include the helpers from the view model.
|
|
11
12
|
#
|
|
@@ -19,12 +20,12 @@ module ViewModels
|
|
|
19
20
|
def render_with options
|
|
20
21
|
render options.to_render_options
|
|
21
22
|
end
|
|
22
|
-
|
|
23
|
-
#
|
|
23
|
+
|
|
24
|
+
# Rails 3 calls it with 2 arguments
|
|
24
25
|
#
|
|
25
|
-
def find_template path
|
|
26
|
-
|
|
26
|
+
def find_template path, second=nil
|
|
27
|
+
lookup_context.find_template path rescue nil
|
|
27
28
|
end
|
|
28
|
-
|
|
29
|
+
|
|
29
30
|
end
|
|
30
31
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ViewModels::Base do
|
|
4
4
|
|
|
@@ -53,16 +53,49 @@ describe ViewModels::Base do
|
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
describe "to_param" do
|
|
57
|
+
before(:each) do
|
|
58
|
+
@model = stub :model
|
|
59
|
+
@view_model = ViewModels::Base.new @model, nil
|
|
60
|
+
end
|
|
61
|
+
it "should delegate to_param to the model" do
|
|
62
|
+
@model.should_receive(:to_param).once
|
|
63
|
+
|
|
64
|
+
@view_model.to_param
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "should delegate id to the model" do
|
|
68
|
+
@model.should_receive(:id).once
|
|
69
|
+
|
|
70
|
+
@view_model.id
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "should delegate dom_id with no args to ActionController::RecordIdentifier" do
|
|
74
|
+
ActionController::RecordIdentifier.should_receive(:dom_id).once
|
|
75
|
+
|
|
76
|
+
@view_model.dom_id
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "should delegate dom_id with args to the context" do
|
|
80
|
+
@context = mock('context')
|
|
81
|
+
@view_model.stub! :context => @context
|
|
82
|
+
@context.should_receive(:dom_id).with('arg1', 'arg2').once
|
|
83
|
+
|
|
84
|
+
@view_model.dom_id('arg1', 'arg2')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
end
|
|
88
|
+
|
|
56
89
|
describe ".master_helper_module" do
|
|
57
90
|
before(:each) do
|
|
58
91
|
class ViewModels::SpecificMasterHelperModule < ViewModels::Base; end
|
|
59
92
|
end
|
|
60
93
|
it "should be a class specific inheritable accessor" do
|
|
61
|
-
ViewModels::SpecificMasterHelperModule.
|
|
62
|
-
ViewModels::SpecificMasterHelperModule.
|
|
94
|
+
ViewModels::SpecificMasterHelperModule._helpers = :some_value
|
|
95
|
+
ViewModels::SpecificMasterHelperModule._helpers.should == :some_value
|
|
63
96
|
end
|
|
64
97
|
it "should be an instance of Module on Base" do
|
|
65
|
-
ViewModels::Base.
|
|
98
|
+
ViewModels::Base._helpers.should be_instance_of(Module)
|
|
66
99
|
end
|
|
67
100
|
end
|
|
68
101
|
|
|
@@ -84,7 +117,7 @@ describe ViewModels::Base do
|
|
|
84
117
|
end
|
|
85
118
|
it "should include the helper in the master helper module" do
|
|
86
119
|
master_helper_module = Module.new
|
|
87
|
-
ViewModels::Base.should_receive(:
|
|
120
|
+
ViewModels::Base.should_receive(:_helpers).and_return master_helper_module
|
|
88
121
|
|
|
89
122
|
helper_module = Module.new
|
|
90
123
|
master_helper_module.should_receive(:include).once.with helper_module
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ViewModels::Extensions::ModelReader do
|
|
4
4
|
|
|
@@ -25,7 +25,7 @@ describe ViewModels::Extensions::ModelReader do
|
|
|
25
25
|
@view_model.some_model_value.should == 's'
|
|
26
26
|
end
|
|
27
27
|
it "should call filters in a given pattern" do
|
|
28
|
-
@view_model.class.model_reader
|
|
28
|
+
@view_model.class.model_reader :some_model_value, :filter_through => [:a, :b, :a, :a]
|
|
29
29
|
|
|
30
30
|
@view_model.some_model_value.should == 'sabaa'
|
|
31
31
|
end
|
|
@@ -34,8 +34,8 @@ describe ViewModels::Extensions::ModelReader do
|
|
|
34
34
|
|
|
35
35
|
lambda { @view_model.some_model_value }.should_not raise_error
|
|
36
36
|
end
|
|
37
|
-
it "should handle
|
|
38
|
-
@view_model.class.model_reader
|
|
37
|
+
it "should handle multiple readers" do
|
|
38
|
+
@view_model.class.model_reader :some_model_value, :some_other_model_value, :filter_through => :a
|
|
39
39
|
|
|
40
40
|
lambda { @view_model.some_model_value }.should_not raise_error
|
|
41
41
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ViewModels::Helpers::Mapping::Collection do
|
|
4
4
|
include ViewModels::Helpers::Mapping
|
|
@@ -9,6 +9,24 @@ describe ViewModels::Helpers::Mapping::Collection do
|
|
|
9
9
|
@collection_view_model = ViewModels::Helpers::Mapping::Collection.new @collection, @context
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
describe "render_partial" do
|
|
13
|
+
it "should call instance eval on the context" do
|
|
14
|
+
@context.should_receive(:instance_eval).once
|
|
15
|
+
|
|
16
|
+
@collection_view_model.send :render_partial, :some_name, :some_params
|
|
17
|
+
end
|
|
18
|
+
it "should render the partial in the 'context' context" do
|
|
19
|
+
@context.should_receive(:render).once
|
|
20
|
+
|
|
21
|
+
@collection_view_model.send :render_partial, :some_name, :some_params
|
|
22
|
+
end
|
|
23
|
+
it "should call render partial on context with the passed through parameters" do
|
|
24
|
+
@context.should_receive(:render).once.with(:partial => 'view_models/collection/some_name', :locals => { :a => :b })
|
|
25
|
+
|
|
26
|
+
@collection_view_model.send :render_partial, :some_name, { :a => :b }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
12
30
|
describe "collection_view_model_for" do
|
|
13
31
|
it "should return kind of a ViewModels::Collection" do
|
|
14
32
|
collection_view_model_for([]).should be_kind_of ViewModels::Helpers::Mapping::Collection
|
|
@@ -119,6 +137,7 @@ describe ViewModels::Helpers::Mapping::Collection do
|
|
|
119
137
|
it "should call render_partial with the right parameters" do
|
|
120
138
|
default_options = {
|
|
121
139
|
:collection => @collection,
|
|
140
|
+
:template_name => :pagination,
|
|
122
141
|
:separator => '|'
|
|
123
142
|
}
|
|
124
143
|
@collection_view_model.should_receive(:render_partial).once.with :pagination, default_options
|
|
@@ -128,6 +147,7 @@ describe ViewModels::Helpers::Mapping::Collection do
|
|
|
128
147
|
it "should override the default options if specific options are given" do
|
|
129
148
|
specific_options = {
|
|
130
149
|
:collection => :a,
|
|
150
|
+
:template_name => :b,
|
|
131
151
|
:separator => :c
|
|
132
152
|
}
|
|
133
153
|
@collection_view_model.should_receive(:render_partial).once.with :pagination, specific_options
|