rlayout 0.4.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/README +1 -1
- data/Rakefile +1 -1
- data/lib/rlayout/layout.rb +79 -11
- data/lib/rlayout/model.rb +77 -77
- data/lib/rlayout_layout.rb +11 -4
- metadata +3 -3
data/CHANGELOG
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
Changes in version 0.5.1 (2009-3-9)
|
2
|
+
support rails 2.3
|
3
|
+
|
4
|
+
Changes in version 0.5.0 (2009-2-18)
|
5
|
+
support rails 2.2.2
|
6
|
+
use !request.get? to replace request.post?
|
7
|
+
hide persist method
|
8
|
+
fix arguments number error of destroy call back
|
9
|
+
use constant for layout parameter name in request
|
10
|
+
add process for protected attributes
|
11
|
+
abstract fetch_model for inheritance
|
12
|
+
|
13
|
+
|
1
14
|
Changes in version 0.4.1 (2008-11-28)
|
2
15
|
-------------------------------------
|
3
16
|
fix bugs about inheritable hash attributes related to model
|
data/README
CHANGED
data/Rakefile
CHANGED
data/lib/rlayout/layout.rb
CHANGED
@@ -1,26 +1,39 @@
|
|
1
1
|
=begin Rlayout
|
2
2
|
author: Leon Li(scorpio_leon@hotmail.com)
|
3
3
|
=end
|
4
|
+
|
4
5
|
module Rlayout
|
5
6
|
module Layout
|
7
|
+
LAYOUT_PARAMETER = :layout if !defined?(LAYOUT_PARAMETER)
|
6
8
|
def self.included(base)
|
7
|
-
|
9
|
+
base.class_eval do
|
10
|
+
#for rails 2.1
|
11
|
+
if method_defined?(:render_with_no_layout)
|
12
|
+
base.class_eval do
|
13
|
+
alias_method :render, :render_with_a_layout_ext
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
#for rails 2.2
|
8
18
|
base.class_eval do
|
9
|
-
alias_method :
|
19
|
+
alias_method :_render_with_layout, :_render_with_layout_with_rlayout
|
10
20
|
end
|
11
21
|
end
|
22
|
+
|
12
23
|
end
|
24
|
+
|
25
|
+
#for rails 2.1
|
13
26
|
def render_with_a_layout_ext(options = nil, extra_options = {}, &block) #:nodoc:
|
14
27
|
template_with_options = options.is_a?(Hash)
|
15
|
-
|
28
|
+
|
16
29
|
#get layout from view
|
17
30
|
options_new = options.dup.merge :layout => false if template_with_options
|
18
31
|
content_for_layout = render_with_no_layout(options_new, extra_options, &block)
|
19
32
|
new_layout = nil
|
20
33
|
page_config = @template.instance_variable_get("@content_for_config")
|
21
|
-
unless page_config.nil?
|
34
|
+
unless page_config.nil?
|
22
35
|
page_config = page_config.strip
|
23
|
-
page_config.split("\n").each do |pair|
|
36
|
+
page_config.split("\n").each do |pair|
|
24
37
|
key, value = pair.split(": ")
|
25
38
|
value = '' if value.nil?
|
26
39
|
if key == 'layout'
|
@@ -30,16 +43,16 @@ module Rlayout
|
|
30
43
|
end
|
31
44
|
end
|
32
45
|
end
|
33
|
-
new_layout = params[
|
46
|
+
new_layout = params[LAYOUT_PARAMETER] if new_layout.nil? || new_layout.empty?
|
34
47
|
unless new_layout.nil? || new_layout.empty?
|
35
|
-
|
36
|
-
options[:layout] = new_layout == '
|
48
|
+
options ||= {}
|
49
|
+
options[:layout] = new_layout == 'false' ? false : new_layout
|
37
50
|
template_with_options = true
|
38
51
|
end
|
39
|
-
|
52
|
+
|
40
53
|
if (layout = pick_layout(template_with_options, options)) && apply_layout?(template_with_options, options)
|
41
54
|
#assert_existence_of_template_file(layout)
|
42
|
-
|
55
|
+
|
43
56
|
options = options.merge :layout => false if template_with_options
|
44
57
|
logger.info("Rendering template within #{layout}") if logger
|
45
58
|
#content_for_layout = render_with_no_layout(options, &block)
|
@@ -54,5 +67,60 @@ module Rlayout
|
|
54
67
|
content_for_layout
|
55
68
|
end
|
56
69
|
end
|
70
|
+
|
71
|
+
#for rails 2.2
|
72
|
+
def _render_with_layout_with_rlayout(options, local_assigns, &block) #:nodoc:
|
73
|
+
partial_layout = options.delete(:layout)
|
74
|
+
|
75
|
+
if block_given?
|
76
|
+
begin
|
77
|
+
@_proc_for_layout = block
|
78
|
+
concat(render(options.merge(:partial => partial_layout)))
|
79
|
+
ensure
|
80
|
+
@_proc_for_layout = nil
|
81
|
+
end
|
82
|
+
else
|
83
|
+
begin
|
84
|
+
|
85
|
+
original_content_for_layout = @content_for_layout if defined?(@content_for_layout)
|
86
|
+
@content_for_layout = render(options)
|
87
|
+
|
88
|
+
#part 1 start to add for extract layout from page
|
89
|
+
pagel_layout = params[LAYOUT_PARAMETER] unless params[LAYOUT_PARAMETER].nil?
|
90
|
+
unless @content_for_config.nil?
|
91
|
+
@content_for_config.strip!
|
92
|
+
@content_for_config.split("\n").each do |pair|
|
93
|
+
key, value = pair.split(": ")
|
94
|
+
value = '' if value.nil?
|
95
|
+
if key == 'layout'
|
96
|
+
pagel_layout = value if pagel_layout.nil? || pagel_layout.empty?
|
97
|
+
else
|
98
|
+
instance_variable_set("@content_for_#{key}", value)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
unless pagel_layout == 'false'
|
104
|
+
partial_layout = active_layout(pagel_layout) if pagel_layout.present?
|
105
|
+
#part 1 end to add for extract layout from page
|
106
|
+
if (options[:inline] || options[:file] || options[:text])
|
107
|
+
@cached_content_for_layout = @content_for_layout
|
108
|
+
render(:file => partial_layout, :locals => local_assigns)
|
109
|
+
else
|
110
|
+
render(options.merge(:partial => partial_layout))
|
111
|
+
end
|
112
|
+
#part 2 start to add for extract layout from page
|
113
|
+
else
|
114
|
+
@content_for_layout
|
115
|
+
end
|
116
|
+
#part 2 end to add for extract layout from page
|
117
|
+
ensure
|
118
|
+
@content_for_layout = original_content_for_layout
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
57
122
|
end
|
58
|
-
end
|
123
|
+
end
|
124
|
+
=begin Rlayout
|
125
|
+
author: Leon Li(scorpio_leon@hotmail.com)
|
126
|
+
=end
|
data/lib/rlayout/model.rb
CHANGED
@@ -7,9 +7,72 @@ module Rlayout
|
|
7
7
|
module Model
|
8
8
|
def self.included(base)
|
9
9
|
base.extend(ClassMethods)
|
10
|
-
base.write_inheritable_attribute(:hidden_actions, base.hidden_actions - ['create', 'update', 'destroy'])
|
11
10
|
end
|
12
|
-
|
11
|
+
|
12
|
+
def create
|
13
|
+
before_create if respond_to?(:before_create)
|
14
|
+
result = self.class.get_action_result(:create)
|
15
|
+
begin
|
16
|
+
execute_create
|
17
|
+
create_success_callback if respond_to?(:create_success_callback)
|
18
|
+
result = result[:success].dup
|
19
|
+
result_type = result.delete(:type)
|
20
|
+
rescue => e
|
21
|
+
create_error_callback(e) if respond_to?(:create_error_callback)
|
22
|
+
result = result[:error].dup
|
23
|
+
end
|
24
|
+
before_create_result if respond_to?(:before_create_result)
|
25
|
+
case result_type.to_s
|
26
|
+
when 'redirect'
|
27
|
+
redirect_to(self.class.parse_result(result, binding))
|
28
|
+
when 'render'
|
29
|
+
render(self.class.parse_result(result, binding))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def update
|
34
|
+
result = self.class.get_action_result(:update)
|
35
|
+
before_update if respond_to?(:before_update)
|
36
|
+
begin
|
37
|
+
execute_update
|
38
|
+
update_success_callback if respond_to?(:update_success_callback)
|
39
|
+
result = result[:success].dup
|
40
|
+
result_type = result.delete(:type)
|
41
|
+
rescue => e
|
42
|
+
update_error_callback(e) if respond_to?(:update_error_callback)
|
43
|
+
result = result[:error].dup
|
44
|
+
end
|
45
|
+
before_update_result if respond_to?(:before_update_result)
|
46
|
+
case result_type.to_s
|
47
|
+
when 'redirect'
|
48
|
+
redirect_to(self.class.parse_result(result, binding))
|
49
|
+
when 'render'
|
50
|
+
render(self.class.parse_result(result, binding))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroy
|
55
|
+
result = self.class.get_action_result(:destroy)
|
56
|
+
before_destroy if respond_to?(:before_destroy)
|
57
|
+
begin
|
58
|
+
execute_destroy
|
59
|
+
destroy_success_callback if respond_to?(:destroy_success_callback)
|
60
|
+
result = result[:success].dup
|
61
|
+
result_type = result.delete(:type)
|
62
|
+
rescue => e
|
63
|
+
destroy_error_callback(e) if respond_to?(:destroy_error_callback)
|
64
|
+
result = result[:error].dup
|
65
|
+
end
|
66
|
+
before_destroy_result if respond_to?(:before_destroy_result)
|
67
|
+
case result_type.to_s
|
68
|
+
when 'redirect'
|
69
|
+
redirect_to(self.class.parse_result(result, binding))
|
70
|
+
when 'render'
|
71
|
+
render(self.class.parse_result(result, binding))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
13
76
|
def find_model_by_id
|
14
77
|
self.class.get_model_class.find(params[:id])
|
15
78
|
end
|
@@ -35,24 +98,22 @@ module Rlayout
|
|
35
98
|
end
|
36
99
|
end
|
37
100
|
end
|
101
|
+
|
102
|
+
def fetch_model
|
103
|
+
if params[:id].present?
|
104
|
+
@model = find_model_by_id
|
105
|
+
else
|
106
|
+
@model = self.class.get_model_class.new
|
107
|
+
end
|
108
|
+
end
|
38
109
|
|
39
110
|
def set_model
|
40
111
|
if need_model?
|
41
112
|
@model = nil
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
@model.attributes=params[params_key] unless @model.nil? || params[params_key].nil?
|
47
|
-
end
|
48
|
-
else
|
49
|
-
if request.post?
|
50
|
-
params_key = self.class.get_full_model_name.gsub(/\//, "_").to_sym
|
51
|
-
#notice, don't use @model.attributes= after new due to mock require
|
52
|
-
@model = self.class.get_model_class.new(params[params_key])
|
53
|
-
else
|
54
|
-
@model = self.class.get_model_class.new
|
55
|
-
end
|
113
|
+
fetch_model
|
114
|
+
if @model && !request.get?
|
115
|
+
params_key = self.class.get_full_model_name.gsub(/\//, "_").to_sym
|
116
|
+
@model.attributes=params[params_key] unless @model.nil? || params[params_key].nil?
|
56
117
|
end
|
57
118
|
#bind @model to @#{self.class.get_model_name}
|
58
119
|
instance_variable_set("@#{self.class.get_model_name}", @model)
|
@@ -90,68 +151,7 @@ module Rlayout
|
|
90
151
|
end
|
91
152
|
end
|
92
153
|
|
93
|
-
def create
|
94
|
-
before_create if respond_to?(:before_create)
|
95
|
-
result = self.class.get_action_result(:create)
|
96
|
-
begin
|
97
|
-
execute_create
|
98
|
-
create_success_callback if respond_to?(:create_success_callback)
|
99
|
-
result = result[:success].dup
|
100
|
-
result_type = result.delete(:type)
|
101
|
-
rescue => e
|
102
|
-
create_error_callback(e) if respond_to?(:create_error_callback)
|
103
|
-
result = result[:error].dup
|
104
|
-
end
|
105
|
-
before_create_result if respond_to?(:before_create_result)
|
106
|
-
case result_type.to_s
|
107
|
-
when 'redirect'
|
108
|
-
redirect_to(self.class.parse_result(result, binding))
|
109
|
-
when 'render'
|
110
|
-
render(self.class.parse_result(result, binding))
|
111
|
-
end
|
112
|
-
end
|
113
154
|
|
114
|
-
def update
|
115
|
-
result = self.class.get_action_result(:update)
|
116
|
-
before_update if respond_to?(:before_update)
|
117
|
-
begin
|
118
|
-
execute_update
|
119
|
-
update_success_callback if respond_to?(:update_success_callback)
|
120
|
-
result = result[:success].dup
|
121
|
-
result_type = result.delete(:type)
|
122
|
-
rescue => e
|
123
|
-
update_error_callback(e) if respond_to?(:update_error_callback)
|
124
|
-
result = result[:error].dup
|
125
|
-
end
|
126
|
-
before_update_result if respond_to?(:before_update_result)
|
127
|
-
case result_type.to_s
|
128
|
-
when 'redirect'
|
129
|
-
redirect_to(self.class.parse_result(result, binding))
|
130
|
-
when 'render'
|
131
|
-
render(self.class.parse_result(result, binding))
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def destroy
|
136
|
-
result = self.class.get_action_result(:destroy)
|
137
|
-
before_destroy if respond_to?(:before_destroy)
|
138
|
-
begin
|
139
|
-
execute_destroy
|
140
|
-
destroy_success_callback(e) if respond_to?(:destroy_success_callback)
|
141
|
-
result = result[:success].dup
|
142
|
-
result_type = result.delete(:type)
|
143
|
-
rescue => e
|
144
|
-
destroy_error_callback if respond_to?(:destroy_error_callback)
|
145
|
-
result = result[:error].dup
|
146
|
-
end
|
147
|
-
before_destroy_result if respond_to?(:before_destroy_result)
|
148
|
-
case result_type.to_s
|
149
|
-
when 'redirect'
|
150
|
-
redirect_to(self.class.parse_result(result, binding))
|
151
|
-
when 'render'
|
152
|
-
render(self.class.parse_result(result, binding))
|
153
|
-
end
|
154
|
-
end
|
155
155
|
|
156
156
|
module ClassMethods
|
157
157
|
|
data/lib/rlayout_layout.rb
CHANGED
@@ -3,7 +3,14 @@ author: Leon Li(scorpio_leon@hotmail.com)
|
|
3
3
|
=end
|
4
4
|
require 'rlayout/layout'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
if RAILS_GEM_VERSION >= '2.2'
|
7
|
+
ActionView::Base.class_eval do
|
8
|
+
#for rails 2.2
|
9
|
+
include Rlayout::Layout
|
10
|
+
end
|
11
|
+
else
|
12
|
+
ActionController::Base.class_eval do
|
13
|
+
#for rails 2.1
|
14
|
+
include Rlayout::Layout
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rlayout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leon Li
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-03-09 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
requirements: []
|
70
70
|
|
71
71
|
rubyforge_project: Rails Layout Extension
|
72
|
-
rubygems_version: 1.
|
72
|
+
rubygems_version: 1.3.1
|
73
73
|
signing_key:
|
74
74
|
specification_version: 2
|
75
75
|
summary: improve rails layout such as simplifying content_for usage and let erb file can determine layout
|