view_models 1.5.7 → 2.0.0.ruby19
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/init.rb +1 -0
- data/lib/padrino/README.textile +3 -0
- data/lib/padrino/init.rb +10 -0
- data/lib/padrino/lib/helpers/collection.rb +27 -0
- data/lib/padrino/lib/padrino/view_models.rb +19 -0
- data/lib/padrino/lib/view_models/base.rb +65 -0
- data/lib/rails2/README.textile +3 -0
- data/{TODO.textile → lib/rails2/TODO.textile} +0 -0
- data/{generators → lib/rails2/generators}/view_models/USAGE +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/README +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/spec/view_model_spec.rb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/view_models/view_model.rb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/_empty.html.haml +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_collection.html.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_collection.html.haml +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_collection.text.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_list.html.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_list.text.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_pagination.html.haml +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_pagination.text.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_table.html.haml +0 -0
- data/{generators → lib/rails2/generators}/view_models/templates/views/view_models/collection/_table.text.erb +0 -0
- data/{generators → lib/rails2/generators}/view_models/view_models_generator.rb +0 -0
- data/lib/rails2/init.rb +18 -0
- data/lib/{experimental → rails2/lib/experimental}/modules_in_render_hierarchy.rb +0 -0
- data/lib/{extensions → rails2/lib/extensions}/view.rb +0 -0
- data/lib/rails2/lib/helpers/collection.rb +27 -0
- data/lib/{helpers → rails2/lib/helpers}/view.rb +0 -0
- data/lib/rails2/lib/view_models.rb +1 -0
- data/lib/rails2/lib/view_models/base.rb +99 -0
- data/lib/{view_models → rails2/lib/view_models}/view.rb +5 -1
- data/lib/shared/README.textile +3 -0
- data/lib/shared/init.rb +11 -0
- data/lib/shared/lib/view_models.rb +4 -0
- data/lib/{view_models → shared/lib/view_models}/base.rb +46 -91
- data/lib/{view_models/controller_extractor.rb → shared/lib/view_models/context_extractor.rb} +4 -2
- data/lib/{extensions → shared/lib/view_models/extensions}/active_record.rb +2 -0
- data/lib/{extensions → shared/lib/view_models/extensions}/model_reader.rb +1 -1
- data/lib/{helpers → shared/lib/view_models/helpers}/collection.rb +1 -13
- data/lib/{helpers/rails.rb → shared/lib/view_models/helpers/mapping.rb} +5 -1
- data/lib/{view_models → shared/lib/view_models}/path_store.rb +0 -0
- data/lib/{view_models → shared/lib/view_models}/render_options.rb +4 -0
- data/lib/view_models.rb +5 -3
- metadata +60 -109
- data/CHANGELOG +0 -26
- data/MIT-LICENSE +0 -20
- data/README.textile +0 -40
- data/Rakefile +0 -49
- data/VERSION.yml +0 -4
- data/generators/view_models/templates/views/view_models/collection/_list.html.haml +0 -7
- data/lib/experimental/README.textile +0 -32
- data/rails/init.rb +0 -18
- data/spec/integration/integration_spec.rb +0 -281
- data/spec/integration/models/sub_subclass.rb +0 -14
- data/spec/integration/models/subclass.rb +0 -3
- data/spec/integration/view_models/module_for_rendering.rb +0 -7
- data/spec/integration/view_models/project.rb +0 -14
- data/spec/integration/view_models/sub_subclass.rb +0 -42
- data/spec/integration/view_models/subclass.rb +0 -1
- data/spec/integration/views/view_models/collection/_collection.html.erb +0 -1
- data/spec/integration/views/view_models/collection/_collection.text.erb +0 -6
- data/spec/integration/views/view_models/collection/_list.html.erb +0 -1
- data/spec/integration/views/view_models/collection/_list.text.erb +0 -6
- data/spec/integration/views/view_models/module_for_rendering/_not_found_in_sub_subclass_but_in_module.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_capture_in_template.erb +0 -2
- data/spec/integration/views/view_models/sub_subclass/_capture_in_view_model.erb +0 -3
- data/spec/integration/views/view_models/sub_subclass/_collection_example.html.erb +0 -3
- data/spec/integration/views/view_models/sub_subclass/_collection_example.text.erb +0 -3
- data/spec/integration/views/view_models/sub_subclass/_collection_item.html.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_collection_item.text.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_exists.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_exists.html.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_exists.text.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_exists_in_both.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_inner.also_explicit.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_inner.nesting.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_list_example.html.erb +0 -3
- data/spec/integration/views/view_models/sub_subclass/_list_example.text.erb +0 -3
- data/spec/integration/views/view_models/sub_subclass/_list_item.html.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_list_item.text.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_outer.explicit.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_outer.nesting.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/_part_that_is_dependent_on_the_view_model.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/show.html.erb +0 -1
- data/spec/integration/views/view_models/sub_subclass/show.text.erb +0 -1
- data/spec/integration/views/view_models/subclass/_exists_in_both.erb +0 -1
- data/spec/integration/views/view_models/subclass/_no_sub_subclass.erb +0 -1
- data/spec/integration/views/view_models/subclass/_not_found_in_sub_subclass.erb +0 -1
- data/spec/lib/extensions/active_record_spec.rb +0 -31
- data/spec/lib/extensions/model_reader_spec.rb +0 -93
- data/spec/lib/helpers/collection_spec.rb +0 -196
- data/spec/lib/helpers/rails_spec.rb +0 -84
- data/spec/lib/helpers/view_spec.rb +0 -20
- data/spec/lib/view_models/base_spec.rb +0 -102
- data/spec/lib/view_models/view_spec.rb +0 -9
- data/spec/spec_helper.rb +0 -14
- data/spec/spec_helper_extensions.rb +0 -13
data/lib/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '/view_models')
|
data/lib/padrino/init.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
this = File.dirname __FILE__
|
2
|
+
|
3
|
+
require File.join(this, '/../shared/init')
|
4
|
+
|
5
|
+
ViewModels::Helpers::Mapping
|
6
|
+
|
7
|
+
require File.join(this, '/lib/view_models/base')
|
8
|
+
require File.join(this, '/lib/helpers/collection')
|
9
|
+
|
10
|
+
require File.join(this, '/lib/padrino/view_models')
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ViewModels
|
2
|
+
module Helpers
|
3
|
+
module Mapping
|
4
|
+
|
5
|
+
# The Collection view_model helper has the purpose of presenting presentable collections.
|
6
|
+
# * Render as list
|
7
|
+
# * Render as table
|
8
|
+
# * Render as collection
|
9
|
+
# * Render a pagination
|
10
|
+
#
|
11
|
+
class Collection
|
12
|
+
private
|
13
|
+
|
14
|
+
# Helper method that renders a partial in the context of the context instance.
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
# If the collection view_model helper has been instantiated in the context
|
18
|
+
# of a controller, render will be called in the controller.
|
19
|
+
#
|
20
|
+
def render_partial name, locals
|
21
|
+
@context.instance_eval { render "view_models/collection/_#{name}", :locals => locals }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Padrino
|
2
|
+
module ViewModels
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def registered app
|
6
|
+
app.send :include, ::ViewModels::Helpers::Mapping
|
7
|
+
|
8
|
+
Padrino.set_load_paths File.join(app.root, "/view_models")
|
9
|
+
|
10
|
+
Padrino.require_dependencies File.join(app.root, "/view_models.rb")
|
11
|
+
# FIXME Needs to require in a specific order.
|
12
|
+
#
|
13
|
+
Padrino.require_dependencies File.join(app.root, "/view_models/**/*.rb")
|
14
|
+
end
|
15
|
+
alias :included :registered
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Base Module for Padrino ViewModels.
|
2
|
+
#
|
3
|
+
module ViewModels
|
4
|
+
|
5
|
+
# Base class from which all view_models inherit.
|
6
|
+
#
|
7
|
+
class Base
|
8
|
+
|
9
|
+
# Make helper and helper_method available
|
10
|
+
#
|
11
|
+
# TODO Rewrite Padrino style.
|
12
|
+
#
|
13
|
+
# include ActionController::Helpers
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
# Alias the context_method to the padrino-centric app_method.
|
18
|
+
#
|
19
|
+
alias app_method context_method
|
20
|
+
|
21
|
+
# Sets the view format and tries to render the given options.
|
22
|
+
#
|
23
|
+
# Note: Also caches [path, name, format] => template path.
|
24
|
+
#
|
25
|
+
def render renderer, options
|
26
|
+
# options.format! view
|
27
|
+
renderer.instance_variable_set(:@_content_type, options.format || :html)
|
28
|
+
path_store.cached options do
|
29
|
+
path = template_path renderer, options
|
30
|
+
options.file = path
|
31
|
+
renderer.send :render, path.to_s, options.to_render_options
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# FIXME Need to use padrino register for this.
|
36
|
+
#
|
37
|
+
alias_method :register, :include
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
# Accesses the view to find a suitable template path.
|
42
|
+
#
|
43
|
+
# Returns nil if a template cannot be found.
|
44
|
+
#
|
45
|
+
def template_path_from renderer, options
|
46
|
+
template = renderer.send :resolve_template, tentative_template_path(options)
|
47
|
+
# TODO!
|
48
|
+
#
|
49
|
+
template && template.first.to_s # was .path
|
50
|
+
rescue Padrino::Rendering::TemplateNotFound => t
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
alias app context
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def renderer
|
61
|
+
app
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/rails2/init.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# require 'experimental/modules_in_render_hierarchy'
|
2
|
+
|
3
|
+
this = File.dirname __FILE__
|
4
|
+
|
5
|
+
require File.join(this, '/lib/view_models')
|
6
|
+
require File.join(this, '/../shared/init')
|
7
|
+
require File.join(this, '/lib/view_models/base')
|
8
|
+
require File.join(this, '/lib/view_models/view')
|
9
|
+
|
10
|
+
require File.join(this, '/lib/helpers/view')
|
11
|
+
require File.join(this, '/lib/helpers/collection')
|
12
|
+
|
13
|
+
# TODO How was the view models generator included?
|
14
|
+
#
|
15
|
+
# require File.join(this, '/generators/view_models/view_models_generator')
|
16
|
+
|
17
|
+
ActionController::Base.send :include, ViewModels::Helpers::Mapping
|
18
|
+
ActionView::Base.send :include, ViewModels::Helpers::Mapping
|
File without changes
|
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ViewModels
|
2
|
+
module Helpers
|
3
|
+
module Mapping
|
4
|
+
|
5
|
+
# The Collection view_model helper has the purpose of presenting presentable collections.
|
6
|
+
# * Render as list
|
7
|
+
# * Render as table
|
8
|
+
# * Render as collection
|
9
|
+
# * Render a pagination
|
10
|
+
#
|
11
|
+
class Collection
|
12
|
+
private
|
13
|
+
|
14
|
+
# Helper method that renders a partial in the context of the context instance.
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
# If the collection view_model helper has been instantiated in the context
|
18
|
+
# of a controller, render will be called in the controller.
|
19
|
+
#
|
20
|
+
def render_partial name, locals
|
21
|
+
@context.instance_eval { render :partial => "view_models/collection/#{name}", :locals => locals }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '/../../shared/lib/view_models')
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# Base Module for Rails 2 ViewModels.
|
2
|
+
#
|
3
|
+
module ViewModels
|
4
|
+
|
5
|
+
# Base class from which all view_models inherit.
|
6
|
+
#
|
7
|
+
class Base
|
8
|
+
|
9
|
+
# Make helper and helper_method available
|
10
|
+
#
|
11
|
+
include ActionController::Helpers
|
12
|
+
|
13
|
+
# This is really only needed because some Rails helpers access
|
14
|
+
# @controller directly.
|
15
|
+
# It's really bad.
|
16
|
+
#
|
17
|
+
# TODO Make it call super.
|
18
|
+
#
|
19
|
+
def initialize model, controller_or_view
|
20
|
+
@model = model
|
21
|
+
@context = @controller = ContextExtractor.new(controller_or_view).extract
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
|
26
|
+
# Alias the context_method to the rails-centric controller_method.
|
27
|
+
#
|
28
|
+
alias controller_method context_method
|
29
|
+
|
30
|
+
# Wrapper for add_template_helper in ActionController::Helpers, also
|
31
|
+
# includes given helper in the view_model
|
32
|
+
#
|
33
|
+
# TODO extract into module
|
34
|
+
#
|
35
|
+
unless instance_methods.include?('old_add_template_helper')
|
36
|
+
alias old_add_template_helper add_template_helper
|
37
|
+
def add_template_helper helper_module
|
38
|
+
include helper_module
|
39
|
+
old_add_template_helper helper_module
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Sets the view format and tries to render the given options.
|
44
|
+
#
|
45
|
+
# Note: Also caches [path, name, format] => template path.
|
46
|
+
#
|
47
|
+
def render renderer, options
|
48
|
+
options.format! renderer
|
49
|
+
path_store.cached options do
|
50
|
+
options.file = template_path renderer, options
|
51
|
+
renderer.render_with options
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
# Accesses the view to find a suitable template path.
|
58
|
+
#
|
59
|
+
def template_path_from renderer, options
|
60
|
+
template = renderer.find_template tentative_template_path(options)
|
61
|
+
|
62
|
+
template && template.path
|
63
|
+
end
|
64
|
+
|
65
|
+
end # class << self
|
66
|
+
|
67
|
+
# Delegate context methods.
|
68
|
+
#
|
69
|
+
context_method :form_authenticity_token, :protect_against_forgery?, :request_forgery_protection_token
|
70
|
+
|
71
|
+
# Make all the dynamically generated routes (restful routes etc.)
|
72
|
+
# available in the view_model
|
73
|
+
#
|
74
|
+
ActionController::Routing::Routes.install_helpers self
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
alias controller context
|
79
|
+
|
80
|
+
# CaptureHelper needs this.
|
81
|
+
#
|
82
|
+
attr_accessor :output_buffer
|
83
|
+
|
84
|
+
# Returns a view instance for render_xxx.
|
85
|
+
#
|
86
|
+
# TODO Try getting a view instance from the controller.
|
87
|
+
#
|
88
|
+
def renderer
|
89
|
+
# view = if controller.response.template
|
90
|
+
# controller.response.template
|
91
|
+
# else
|
92
|
+
View.new controller, master_helper_module
|
93
|
+
# end
|
94
|
+
|
95
|
+
# view.extend Extensions::View
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
@@ -3,10 +3,14 @@ module ViewModels
|
|
3
3
|
#
|
4
4
|
class View < ActionView::Base
|
5
5
|
|
6
|
+
# Shut up, opinionated funkers.
|
7
|
+
#
|
8
|
+
alias singleton_class metaclass unless instance_methods.include?('singleton_class')
|
9
|
+
|
6
10
|
# Include the helpers from the view model.
|
7
11
|
#
|
8
12
|
def initialize controller, master_helper_module
|
9
|
-
|
13
|
+
singleton_class.send :include, master_helper_module
|
10
14
|
super controller.class.view_paths, {}, controller
|
11
15
|
end
|
12
16
|
|
data/lib/shared/init.rb
ADDED
@@ -0,0 +1,11 @@
|
|
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')
|
@@ -11,22 +11,14 @@ module ViewModels
|
|
11
11
|
#
|
12
12
|
class Base
|
13
13
|
|
14
|
-
# Model and Controller are accessible from outside.
|
15
|
-
#
|
16
|
-
# TODO but they actually shouldn't be. Try to migrate into protected area.
|
17
|
-
#
|
18
|
-
attr_reader :model, :controller
|
19
|
-
|
20
|
-
# Make helper and helper_method available
|
21
|
-
#
|
22
|
-
include ActionController::Helpers
|
23
|
-
|
24
14
|
# Create a view_model. To create a view_model, you need to have a model (to present) and a context.
|
25
|
-
# The context is usually a view
|
15
|
+
# The context is usually a view, a controller, or an app, but doesn't need to be.
|
16
|
+
#
|
17
|
+
# TODO Include this.
|
26
18
|
#
|
27
|
-
def initialize model,
|
28
|
-
@model
|
29
|
-
@
|
19
|
+
def initialize model, app_or_controller_or_view
|
20
|
+
@model = model
|
21
|
+
@context = ContextExtractor.new(app_or_controller_or_view).extract
|
30
22
|
end
|
31
23
|
|
32
24
|
class << self
|
@@ -42,51 +34,31 @@ module ViewModels
|
|
42
34
|
super
|
43
35
|
end
|
44
36
|
|
45
|
-
#
|
46
|
-
# model method delegation.
|
47
|
-
#
|
48
|
-
include Extensions::ModelReader
|
49
|
-
|
50
|
-
# Delegates method calls to the controller.
|
37
|
+
# Delegates method calls to the context.
|
51
38
|
#
|
52
39
|
# Examples:
|
53
|
-
#
|
54
|
-
#
|
40
|
+
# context_method :current_user
|
41
|
+
# context_method :current_user, :current_profile # multiple methods to be delegated
|
55
42
|
#
|
56
43
|
# In the view_model:
|
57
44
|
# self.current_user
|
58
45
|
# will call
|
59
|
-
#
|
46
|
+
# context.current_user
|
60
47
|
#
|
61
|
-
def
|
62
|
-
delegate *methods << { :to => :
|
48
|
+
def context_method *methods
|
49
|
+
delegate *methods << { :to => :context }
|
63
50
|
end
|
64
51
|
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# TODO extract into module
|
69
|
-
#
|
70
|
-
alias old_add_template_helper add_template_helper
|
71
|
-
def add_template_helper helper_module
|
72
|
-
include helper_module
|
73
|
-
old_add_template_helper helper_module
|
74
|
-
end
|
75
|
-
|
76
|
-
# Sets the view format and tries to render the given options.
|
77
|
-
#
|
78
|
-
# Note: Also caches [path, name, format] => template path.
|
52
|
+
# Installs the model_reader Method for filtered
|
53
|
+
# model method delegation.
|
79
54
|
#
|
80
|
-
|
81
|
-
options.format! view
|
82
|
-
path_store.cached options do
|
83
|
-
options.file = template_path view, options
|
84
|
-
view.render_with options
|
85
|
-
end
|
86
|
-
end
|
55
|
+
include Extensions::ModelReader
|
87
56
|
|
88
57
|
protected
|
89
58
|
|
59
|
+
# Hierarchical rendering.
|
60
|
+
#
|
61
|
+
|
90
62
|
# Returns the next view model class in the render hierarchy.
|
91
63
|
#
|
92
64
|
# Note: Just returns the superclass.
|
@@ -96,13 +68,11 @@ module ViewModels
|
|
96
68
|
def next_in_render_hierarchy
|
97
69
|
superclass
|
98
70
|
end
|
99
|
-
|
100
71
|
# Just raises a fitting template error.
|
101
72
|
#
|
102
73
|
def raise_template_error_with message
|
103
74
|
raise MissingTemplateError.new "No template #{message} found."
|
104
75
|
end
|
105
|
-
|
106
76
|
# Check if the view lookup inheritance chain has ended.
|
107
77
|
#
|
108
78
|
# Raises a MissingTemplateError if yes.
|
@@ -111,6 +81,10 @@ module ViewModels
|
|
111
81
|
self == ViewModels::Base
|
112
82
|
end
|
113
83
|
|
84
|
+
|
85
|
+
# Template finding
|
86
|
+
#
|
87
|
+
|
114
88
|
# Returns a template path for the view with the given options.
|
115
89
|
#
|
116
90
|
# If no template is found, traverses up the inheritance chain.
|
@@ -118,24 +92,20 @@ module ViewModels
|
|
118
92
|
# Raises a MissingTemplateError if none is found during
|
119
93
|
# inheritance chain traversal.
|
120
94
|
#
|
121
|
-
def template_path
|
95
|
+
def template_path renderer, options
|
122
96
|
raise_template_error_with options.error_message if inheritance_chain_ends?
|
123
97
|
|
124
|
-
template_path_from(
|
98
|
+
template_path_from(renderer, options) || self.next_in_render_hierarchy.template_path(renderer, options)
|
125
99
|
end
|
126
100
|
|
127
|
-
#
|
128
|
-
#
|
129
|
-
def template_path_from view, options
|
130
|
-
template = view.find_template tentative_template_path(options)
|
131
|
-
|
132
|
-
template && template.path
|
133
|
-
end
|
101
|
+
# ...
|
134
102
|
|
135
103
|
# Return as render path either a stored path or a newly generated one.
|
136
104
|
#
|
137
105
|
# If nothing or nil is passed, the store is ignored.
|
138
106
|
#
|
107
|
+
# TODO Think about using the built-in Padrino template store.
|
108
|
+
#
|
139
109
|
def tentative_template_path options
|
140
110
|
path_store[options.path_key] || generate_template_path_from(options)
|
141
111
|
end
|
@@ -168,22 +138,17 @@ module ViewModels
|
|
168
138
|
@view_model_path || @view_model_path = self.name.underscore
|
169
139
|
end
|
170
140
|
|
171
|
-
end
|
172
|
-
|
173
|
-
# Delegate controller methods.
|
174
|
-
#
|
175
|
-
controller_method :logger, :form_authenticity_token, :protect_against_forgery?, :request_forgery_protection_token
|
141
|
+
end
|
176
142
|
|
177
|
-
#
|
178
|
-
# available in the view_model
|
143
|
+
# Delegate context methods.
|
179
144
|
#
|
180
|
-
|
145
|
+
context_method :logger
|
181
146
|
|
182
147
|
# Renders the given partial in the view_model's view root in the format given.
|
183
148
|
#
|
184
149
|
# Example:
|
185
|
-
#
|
186
|
-
#
|
150
|
+
# views/view_models/this/view_model/_partial.haml
|
151
|
+
# views/view_models/this/view_model/_partial.text.erb
|
187
152
|
#
|
188
153
|
# The following options are supported:
|
189
154
|
# * :format - Calling view_model.render_as('partial') will render the haml
|
@@ -194,7 +159,7 @@ module ViewModels
|
|
194
159
|
# * If no format is given, it will render the default format, which is (currently) html.
|
195
160
|
#
|
196
161
|
def render_as name, options = {}
|
197
|
-
|
162
|
+
render_internal RenderOptions::Partial.new(name, options)
|
198
163
|
end
|
199
164
|
# render_the is used for small parts.
|
200
165
|
#
|
@@ -208,8 +173,8 @@ module ViewModels
|
|
208
173
|
# Renders the given template in the view_model's view root in the format given.
|
209
174
|
#
|
210
175
|
# Example:
|
211
|
-
#
|
212
|
-
#
|
176
|
+
# views/view_models/this/view_model/template.haml
|
177
|
+
# views/view_models/this/view_model/template name.text.erb
|
213
178
|
#
|
214
179
|
# The following options are supported:
|
215
180
|
# * :format - Calling view_model.render_template('template') will render the haml
|
@@ -220,38 +185,29 @@ module ViewModels
|
|
220
185
|
# * If no format is given, it will render the default format, which is (currently) html.
|
221
186
|
#
|
222
187
|
def render_template name, options = {}
|
223
|
-
|
188
|
+
render_internal RenderOptions::Template.new(name, options)
|
224
189
|
end
|
225
190
|
|
226
191
|
protected
|
227
192
|
|
228
|
-
#
|
193
|
+
# Model and Context (Controller) are accessible from a view model.
|
194
|
+
#
|
195
|
+
# If you really need to call
|
196
|
+
# view_model.model.bla
|
197
|
+
# in a view, I urge you to reconsider. But feel free to make
|
198
|
+
# the methods model, controller, context public.
|
229
199
|
#
|
230
|
-
|
200
|
+
attr_reader :model, :context
|
231
201
|
|
232
202
|
# Internal render method that uses the options to get a view instance
|
233
203
|
# and then referring to its class for rendering.
|
234
204
|
#
|
235
|
-
def
|
205
|
+
def render_internal options
|
236
206
|
options.view_model = self
|
237
207
|
|
238
208
|
determine_and_set_format options
|
239
|
-
|
240
|
-
self.class.render
|
241
|
-
end
|
242
|
-
|
243
|
-
# Returns a view instance for render_xxx.
|
244
|
-
#
|
245
|
-
# TODO Try getting a view instance from the controller.
|
246
|
-
#
|
247
|
-
def view_instance
|
248
|
-
# view = if controller.response.template
|
249
|
-
# controller.response.template
|
250
|
-
# else
|
251
|
-
View.new controller, master_helper_module
|
252
|
-
# end
|
253
|
-
|
254
|
-
# view.extend Extensions::View
|
209
|
+
|
210
|
+
self.class.render renderer, options
|
255
211
|
end
|
256
212
|
|
257
213
|
# Determines what format to use for rendering.
|
@@ -263,6 +219,5 @@ module ViewModels
|
|
263
219
|
def determine_and_set_format options
|
264
220
|
options.format = @template_format = options.format || @template_format
|
265
221
|
end
|
266
|
-
|
267
222
|
end
|
268
223
|
end
|