rlayout 0.3.4 → 0.4.1

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/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ Changes in version 0.4.1 (2008-11-28)
2
+ -------------------------------------
3
+ fix bugs about inheritable hash attributes related to model
4
+
5
+ Changes in version 0.4.0 (2008-11-25)
6
+ -------------------------------------
7
+ new feature support to auto-fetch or create model
8
+
1
9
  Changes in version 0.3.4 (2008-07-10)
2
10
  -------------------------------------
3
11
  support rails 2.1
data/MIT-LICENSE CHANGED
File without changes
data/README CHANGED
@@ -1,4 +1,4 @@
1
- Rails Layout Extension, release 0.3.4 (Jul. 2008)
1
+ Rails Layout Extension, release 0.4.1 (Nov. 2008)
2
2
 
3
3
  Feature
4
4
  =======
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
  PKG_NAME = "rlayout"
4
- PKG_VERSION = "0.3.4"
4
+ PKG_VERSION = "0.4.1"
5
5
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
6
6
  PKG_FILES = FileList[
7
7
  '[A-Z]*',
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,13 @@
1
+ =begin Rlayout
2
+ author: Leon Li(scorpio_leon@hotmail.com)
3
+ =end
4
+ class Object
5
+ def class_simple_name
6
+ if self.is_a?(Class)
7
+ fullname = self.to_s
8
+ else
9
+ fullname = self.class.to_s
10
+ end
11
+ fullname.scan(/(.*::)*([^\(]*)/)[0][1]
12
+ end
13
+ end
@@ -10,18 +10,19 @@ module Rlayout
10
10
  end
11
11
  end
12
12
  end
13
- def render_with_a_layout_ext(options = nil, &block) #:nodoc:
13
+ def render_with_a_layout_ext(options = nil, extra_options = {}, &block) #:nodoc:
14
14
  template_with_options = options.is_a?(Hash)
15
15
 
16
16
  #get layout from view
17
17
  options_new = options.dup.merge :layout => false if template_with_options
18
- content_for_layout = render_with_no_layout(options_new, &block)
18
+ content_for_layout = render_with_no_layout(options_new, extra_options, &block)
19
19
  new_layout = nil
20
20
  page_config = @template.instance_variable_get("@content_for_config")
21
21
  unless page_config.nil?
22
22
  page_config = page_config.strip
23
23
  page_config.split("\n").each do |pair|
24
24
  key, value = pair.split(": ")
25
+ value = '' if value.nil?
25
26
  if key == 'layout'
26
27
  new_layout = value
27
28
  else
@@ -36,7 +37,7 @@ module Rlayout
36
37
  template_with_options = true
37
38
  end
38
39
 
39
- if apply_layout?(template_with_options, options) && (layout = pick_layout(template_with_options, options))
40
+ if (layout = pick_layout(template_with_options, options)) && apply_layout?(template_with_options, options)
40
41
  #assert_existence_of_template_file(layout)
41
42
 
42
43
  options = options.merge :layout => false if template_with_options
@@ -54,4 +55,4 @@ module Rlayout
54
55
  end
55
56
  end
56
57
  end
57
- end
58
+ end
@@ -0,0 +1,255 @@
1
+ =begin Rlayout
2
+ author: Leon Li(scorpio_leon@hotmail.com)
3
+ =end
4
+ #TODO support multi-model
5
+ require 'rlayout/common'
6
+ module Rlayout
7
+ module Model
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ base.write_inheritable_attribute(:hidden_actions, base.hidden_actions - ['create', 'update', 'destroy'])
11
+ end
12
+
13
+ def find_model_by_id
14
+ self.class.get_model_class.find(params[:id])
15
+ end
16
+
17
+ def need_model?
18
+ @action_name_sym = params[:action].to_sym
19
+ self.class.need_model[@action_name_sym] ||= begin
20
+ model_require = self.class.get_model_require
21
+ if model_require.nil?
22
+ false
23
+ else
24
+ if model_require.include?(:none)
25
+ false
26
+ else
27
+ model_require_options = self.class.get_model_require_options
28
+ #TODO use regex
29
+ if model_require_options && model_require_options[:except] && model_require_options[:except].include?(@action_name_sym)
30
+ false
31
+ else
32
+ model_require && (model_require.include?(@action_name_sym) || model_require.include?(:all))
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def set_model
40
+ if need_model?
41
+ @model = nil
42
+ unless params[:id].nil?
43
+ @model = find_model_by_id
44
+ if @model && request.post?
45
+ params_key = self.class.get_full_model_name.gsub(/\//, "_").to_sym
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
56
+ end
57
+ #bind @model to @#{self.class.get_model_name}
58
+ instance_variable_set("@#{self.class.get_model_name}", @model)
59
+ else
60
+ @model = instance_variable_get("@#{self.class.get_model_name}")
61
+ end
62
+
63
+ end
64
+
65
+ def model?
66
+ @model_exists = (@model && @model.id) if @model_exists.nil?
67
+ end
68
+
69
+ def execute_create
70
+ if respond_to?(:create_action)
71
+ create_action
72
+ else
73
+ @model.save!
74
+ end
75
+ end
76
+
77
+ def execute_update
78
+ if respond_to?(:update_action)
79
+ update_action
80
+ else
81
+ @model.save!
82
+ end
83
+ end
84
+
85
+ def execute_destroy
86
+ if respond_to?(:destroy_action)
87
+ destroy_action
88
+ else
89
+ @model.destroy
90
+ end
91
+ end
92
+
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
+
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
+
156
+ module ClassMethods
157
+
158
+ def model_require(*model_require)
159
+ options = model_require.extract_options! || {}
160
+ klass = options.delete(:class)
161
+ model_class(klass) unless klass.nil?
162
+ options.each {|k, v| options[k] = [v] unless v.is_a?(Array)}
163
+ @model_require = model_require
164
+ @model_require_options = options
165
+ end
166
+
167
+ def get_model_require
168
+ @model_require
169
+ end
170
+ def get_model_require_options
171
+ @model_require_options
172
+ end
173
+
174
+ def need_model
175
+ @need_model ||= {}
176
+ end
177
+
178
+ def need_model=(need)
179
+ @need_model = need
180
+ end
181
+
182
+ # get the model_name
183
+ # override this if the model's name is special
184
+ def get_model_name
185
+ @model_name ||= self.class_simple_name.underscore[0..-12].singularize
186
+ end
187
+
188
+ def get_full_model_name
189
+ @model_full_name ||= self.class_full_name.underscore[0..-12].singularize
190
+ end
191
+
192
+ def get_model_class
193
+ @model_class ||= eval("#{get_full_model_name.camelize}")
194
+ end
195
+
196
+ def model_class(model_class)
197
+ @model_class = model_class
198
+ @model_name = model_class.class_simple_name.underscore
199
+ @model_full_name = model_class.class_full_name.underscore
200
+ end
201
+
202
+ #inheritable
203
+ def get_default_results
204
+ read_inheritable_attribute("default_results") || {}
205
+ end
206
+
207
+ #inheritable
208
+ def set_default_result(hash)
209
+ write_inheritable_hash("default_results", hash)
210
+ end
211
+
212
+ #inheritable
213
+ def default_create_result(hash)
214
+ set_default_result(:create => hash)
215
+ end
216
+ def default_update_result(hash)
217
+ set_default_result(:update => hash)
218
+ end
219
+ def default_destroy_result(hash)
220
+ set_default_result(:destroy => hash)
221
+ end
222
+
223
+ def set_action_result(hash)
224
+ @action_results ||= {}
225
+ @action_results.merge!(hash)
226
+ end
227
+
228
+ def create_result(hash)
229
+ set_action_result(:create => hash)
230
+ end
231
+ def update_result(hash)
232
+ set_action_result(:update => hash)
233
+ end
234
+ def destroy_result(hash)
235
+ set_action_result(:destroy => hash)
236
+ end
237
+
238
+ def get_action_result(action)
239
+ if @action_results
240
+ get_default_results.merge(@action_results)[action]
241
+ else
242
+ get_default_results[action]
243
+ end
244
+ end
245
+
246
+ def parse_result(result, binding)
247
+ result.each do |k, v|
248
+ result[k] = eval(v[4..-1], binding) if v.is_a?(String) && v[0, 4] == 'exp:'
249
+ end
250
+ result
251
+ end
252
+
253
+ end
254
+ end
255
+ end
@@ -35,7 +35,7 @@ class Rlayout::TemplateUtil
35
35
  type = 'common' if type.nil?
36
36
  template_key = key+'_'+type
37
37
  @tag_templates ||= {}
38
- if @tag_templates[template_key].nil? || ENV['RAILS_ENV'] != 'production'
38
+ if @tag_templates[template_key].nil? || RAILS_ENV != 'production'
39
39
  template_file = File.join(RAILS_ROOT, 'templates', key, "#{type}.html.erb")
40
40
  template_file = File.join(RAILS_ROOT, 'templates', key, "common.html.erb") unless File.exists?(template_file)
41
41
  @tag_templates[template_key] = File.read(template_file)
data/lib/rlayout.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  =begin Rlayout
2
2
  author: Leon Li(scorpio_leon@hotmail.com)
3
3
  =end
4
+ require 'rlayout_model'
4
5
  require 'rlayout_layout'
5
6
  require 'rlayout_controller'
6
7
  require 'rlayout_template'
@@ -3,7 +3,6 @@ author: Leon Li(scorpio_leon@hotmail.com)
3
3
  =end
4
4
  require 'action_view'
5
5
 
6
-
7
6
  ActionView::Base.class_eval do
8
7
 
9
8
  #for using controller method in view
File without changes
@@ -0,0 +1,9 @@
1
+ =begin Rlayout
2
+ author: Leon Li(scorpio_leon@hotmail.com)
3
+ =end
4
+ require 'rlayout/model'
5
+
6
+ ActionController::Base.class_eval do
7
+ #for page layout
8
+ include Rlayout::Model
9
+ end
File without changes
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.3.4
4
+ version: 0.4.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: 2008-07-11 00:00:00 +08:00
12
+ date: 2008-11-28 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,26 +22,29 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - CHANGELOG
26
25
  - MIT-LICENSE
27
26
  - Rakefile
27
+ - CHANGELOG
28
28
  - README
29
29
  - lib/rlayout
30
- - lib/rlayout/layout.rb
30
+ - lib/rlayout/model.rb
31
31
  - lib/rlayout/template.rb
32
+ - lib/rlayout/layout.rb
33
+ - lib/rlayout/common.rb
34
+ - lib/rlayout_model.rb
35
+ - lib/rlayout_template.rb
32
36
  - lib/rlayout.rb
33
37
  - lib/rlayout_controller.rb
34
38
  - lib/rlayout_layout.rb
35
- - lib/rlayout_template.rb
36
39
  - example/templates
37
40
  - example/templates/simple
38
41
  - example/templates/simple/common.html.erb
39
- - example/templates/simple_mce
40
- - example/templates/simple_mce/text_area.html.erb
41
42
  - example/templates/xhtml
42
- - example/templates/xhtml/common.html.erb
43
43
  - example/templates/xhtml/controlfooter.html.erb
44
+ - example/templates/xhtml/common.html.erb
44
45
  - example/templates/xhtml/controlheader.html.erb
46
+ - example/templates/simple_mce
47
+ - example/templates/simple_mce/text_area.html.erb
45
48
  - example/templates/xhtml_mce
46
49
  - example/templates/xhtml_mce/text_area.html.erb
47
50
  has_rdoc: false
@@ -66,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
69
  requirements: []
67
70
 
68
71
  rubyforge_project: Rails Layout Extension
69
- rubygems_version: 1.1.1
72
+ rubygems_version: 1.2.0
70
73
  signing_key:
71
74
  specification_version: 2
72
75
  summary: improve rails layout such as simplifying content_for usage and let erb file can determine layout