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/init.rb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), '/view_models')
|
data/lib/padrino/README.textile
DELETED
data/lib/padrino/init.rb
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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')
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
|
@@ -1,19 +0,0 @@
|
|
|
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
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
data/lib/rails2/README.textile
DELETED
data/lib/rails2/TODO.textile
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
Usage:
|
|
2
|
-
script/generate view_models <view model class name>
|
|
3
|
-
script/generate view_models User
|
|
4
|
-
|
|
5
|
-
script/generate view_models <view model class name> <partials for render_as, separated by space>
|
|
6
|
-
script/generate view_models User compact extensive list_item table_item
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
View Model generated. Have fun!
|
data/lib/rails2/generators/view_models/templates/views/view_models/collection/_collection.html.erb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<ul class="collection"><%- collection.each do |item| -%><%- view_model = view_model_for item -%><li><%- options = template_format ? { :format => template_format } : {} -%><%= view_model.render_as template_name, options -%></li><%= separator unless item == collection.last %><%- end -%></ul>
|
data/lib/rails2/generators/view_models/templates/views/view_models/collection/_collection.text.erb
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
<%- collection.each do |item| -%>
|
|
2
|
-
<%- view_model = view_model_for item -%>
|
|
3
|
-
<%- options = (template_format ? { :format => template_format } : {}) -%>
|
|
4
|
-
<%= view_model.render_as template_name, options -%>
|
|
5
|
-
<%= separator unless item == collection.entries.last -%>
|
|
6
|
-
<%- end -%>
|
data/lib/rails2/generators/view_models/templates/views/view_models/collection/_list.html.erb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<ol class="collection"><%- collection.each do |item| -%><%- view_model = view_model_for item -%><li><%- options = (template_format ? { :format => template_format } : {}) -%><%= view_model.render_as template_name, options -%></li><%= separator unless item == collection.last %><%- end -%></ol>
|
data/lib/rails2/generators/view_models/templates/views/view_models/collection/_list.text.erb
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
<% collection.each do |item| -%>
|
|
2
|
-
<% view_model = view_model_for item -%>
|
|
3
|
-
<%- options = (template_format ? { :format => template_format } : {}) -%>
|
|
4
|
-
<%= view_model.render_as template_name, options -%>
|
|
5
|
-
<%= separator || '\n' unless item == collection.entries.last -%>
|
|
6
|
-
<% end -%>
|
data/lib/rails2/generators/view_models/templates/views/view_models/collection/_table.text.erb
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
<%- width = 66 -%>
|
|
2
|
-
<%= '-'*width %>
|
|
3
|
-
<% collection.each do |item| -%>
|
|
4
|
-
<% view_model = view_model_for(item, self) -%>
|
|
5
|
-
<%- result = view_model.render_as(template_name) -%>
|
|
6
|
-
<%= '|' + "%-#{width}s"%(result[0,width]) + '|' -%>
|
|
7
|
-
<%- p ('|' + "%-#{width}s"%(result[0,width]) + '|') %>
|
|
8
|
-
<%= separator || ">#{'-'*(width-2)}<" unless item == collection.entries.last %>
|
|
9
|
-
<% end -%>
|
|
10
|
-
<%= '-'*width %>
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
class ViewModelsGenerator < Rails::Generator::NamedBase
|
|
2
|
-
def manifest
|
|
3
|
-
record do |m|
|
|
4
|
-
|
|
5
|
-
# Check for class naming collisions.
|
|
6
|
-
#
|
|
7
|
-
m.class_collisions "ViewModels::#{class_name}"
|
|
8
|
-
|
|
9
|
-
# ViewModels
|
|
10
|
-
#
|
|
11
|
-
m.directory 'app/view_models'
|
|
12
|
-
m.template "view_models/view_model.rb", "app/view_models/#{file_name}.rb"
|
|
13
|
-
|
|
14
|
-
# Specs
|
|
15
|
-
#
|
|
16
|
-
m.directory "spec/app/view_models"
|
|
17
|
-
m.template "spec/view_model_spec.rb", "spec/app/view_models/#{file_name}_spec.rb"
|
|
18
|
-
|
|
19
|
-
# Views
|
|
20
|
-
#
|
|
21
|
-
m.directory "app/views/view_models"
|
|
22
|
-
m.directory "app/views/view_models/#{file_name}"
|
|
23
|
-
actions << 'list_item' if actions.empty?
|
|
24
|
-
actions.each do |action|
|
|
25
|
-
m.template "views/_empty.html.haml", "app/views/view_models/#{file_name}/_#{action}.html.haml"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Copy collection views.
|
|
29
|
-
#
|
|
30
|
-
m.directory "app/views/view_models/collection"
|
|
31
|
-
m.file "views/view_models/collection/_collection.html.haml", "app/views/view_models/collection/_collection.html.haml"
|
|
32
|
-
m.file "views/view_models/collection/_collection.text.erb", "app/views/view_models/collection/_collection.text.erb"
|
|
33
|
-
m.file "views/view_models/collection/_list.html.haml", "app/views/view_models/collection/_list.html.haml"
|
|
34
|
-
m.file "views/view_models/collection/_list.text.erb", "app/views/view_models/collection/_list.text.erb"
|
|
35
|
-
m.file "views/view_models/collection/_pagination.html.haml", "app/views/view_models/collection/_pagination.html.haml"
|
|
36
|
-
m.file "views/view_models/collection/_pagination.text.erb", "app/views/view_models/collection/_pagination.text.erb"
|
|
37
|
-
m.file "views/view_models/collection/_table.html.haml", "app/views/view_models/collection/_table.html.haml"
|
|
38
|
-
m.file "views/view_models/collection/_table.text.erb", "app/views/view_models/collection/_table.text.erb"
|
|
39
|
-
|
|
40
|
-
# Show README.
|
|
41
|
-
#
|
|
42
|
-
m.readme "README"
|
|
43
|
-
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
end
|
data/lib/rails2/init.rb
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
h1. module ModulesInRenderHierarchy
|
|
2
|
-
|
|
3
|
-
Why would we need that?
|
|
4
|
-
|
|
5
|
-
h2. Situation
|
|
6
|
-
|
|
7
|
-
We have a music platform. There we have public profiles, and private profiles. And of each type a band, and a venue profile.
|
|
8
|
-
|
|
9
|
-
Public / Private have different functionality, and also Band / Venue.
|
|
10
|
-
|
|
11
|
-
The chose class structure looks like this:
|
|
12
|
-
|
|
13
|
-
@Public::Base < Project@, @Private::Base < Project@
|
|
14
|
-
@Public::Band < Public::Base@, @Public::Venue < Public::Base@, @Private::Band < Private::Base@, @Private::Venue < Private::Base@
|
|
15
|
-
|
|
16
|
-
To not have duplicate code, we include the functionality for Bands and Venues by using @include BandFunctionality@ and @include VenueFunctionality@.
|
|
17
|
-
|
|
18
|
-
h2. Problem
|
|
19
|
-
|
|
20
|
-
When rendering using the hierarchical rendering, we look up the templates as follows:
|
|
21
|
-
|
|
22
|
-
(For ViewModels::Public::Band#render_as :example, :format => :html)
|
|
23
|
-
# @views/view_models/public/band/_example.html.erb@
|
|
24
|
-
# @views/view_models/public/base/_example.html.erb@
|
|
25
|
-
# @views/view_models/project/_example.html.erb@
|
|
26
|
-
|
|
27
|
-
So the example template is taken from the @public/band@ directory. Why not from a @functionality/band@ directory (assuming the module is named @Functionality::Band@). The reason for this is that the current (v1.5.4) hierarchical rendering process uses superclass, which hops over Modules, rather than using ancestors, which wouldn't.
|
|
28
|
-
|
|
29
|
-
h2. Solution
|
|
30
|
-
|
|
31
|
-
#. Either override superclass, with all associated problems
|
|
32
|
-
#. Or refit the whole hierarchical rendering into a HierarchicalRendering Class. (Preferred)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
module ModulesInRenderHierarchy
|
|
2
|
-
|
|
3
|
-
def self.included klass
|
|
4
|
-
klass.extend ClassMethods
|
|
5
|
-
klass.metaclass.alias_method_chain :include, :superclass_override
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module ClassMethods
|
|
9
|
-
def include_with_superclass_override mod
|
|
10
|
-
original_superclass = superclass
|
|
11
|
-
self.send :include_without_superclass_override, mod
|
|
12
|
-
mod.metaclass.send :define_method, :superclass do
|
|
13
|
-
original_superclass
|
|
14
|
-
end
|
|
15
|
-
metaclass.send :define_method, :superclass do
|
|
16
|
-
mod
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), '/../../shared/lib/view_models')
|
|
@@ -1,99 +0,0 @@
|
|
|
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
|