rlayout 0.2.1 → 0.3.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/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ Changes in version 0.3.0 (2008-06-15)
2
+ -------------------------------------
3
+ support theme template for field tag
4
+
1
5
  Changes in version 0.2.1 (2008-05-22)
2
6
  -------------------------------------
3
7
  remove nested layout include
data/README CHANGED
@@ -1,4 +1,4 @@
1
- Rails Layout Extension, release 0.2.1 (May. 2008)
1
+ Rails Layout Extension, release 0.3.0 (Jun. 2008)
2
2
 
3
3
  Feature
4
4
  =======
@@ -8,13 +8,20 @@ Feature
8
8
  ͨ���ı�Ĭ�ϵ�layout��ȡ��ʽ��ʹerbҲ���Զ���layout������java��sitemesh
9
9
  3. use controller method if helper method requested from erb not exists
10
10
  ERB����helper�ķ��������ڣ����Զ�����controller�ķ���
11
+ 4. support theme template for field tag
12
+ ֧��field tag������ģ��
11
13
 
12
14
  Setup
13
15
  =======
16
+ 1. rlayout as gem
14
17
  put the following line into your rails initializer, or bottom of environment.rb
15
18
  ���������д���ӵ�rails��initializer�����environment.rb�����
16
19
  require 'rlayout'
17
20
 
21
+ 2. rlayout as plugin
22
+ nothing need to do
23
+ ����Ҫ��ʲô��
24
+
18
25
  Usage
19
26
  =======
20
27
 
@@ -25,7 +32,7 @@ title: This is my title
25
32
  panel: This is my panel
26
33
  <% end %>
27
34
 
28
- do the same thing as the following:
35
+ that will do the same thing as the following:
29
36
 
30
37
  <%- content_for :title do -%>
31
38
  This is my title
@@ -40,4 +47,12 @@ This is my panel
40
47
  layout: happy
41
48
  <% end %>
42
49
 
50
+ 3. use theme template of field tag
51
+
52
+ class YourController
53
+ form_theme :xhtml
54
+ end
55
+
56
+ and see related code in folder "example/templates", the theme can be defined in tag with :theme, that will override theme definition in controller
57
+
43
58
  Copyright (c) 2008 Leon Li, released under the MIT license
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
  PKG_NAME = "rlayout"
4
- PKG_VERSION = "0.2.1"
4
+ PKG_VERSION = "0.3.0"
5
5
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
6
6
  PKG_FILES = FileList[
7
7
  '[A-Z]*',
8
- 'lib/**/*'
8
+ 'lib/**/*',
9
+ 'example/**/*'
9
10
  ]
10
11
  spec = Gem::Specification.new do |s|
11
12
  s.platform = Gem::Platform::RUBY
@@ -0,0 +1 @@
1
+ <%= field_context[:body] %>
@@ -0,0 +1 @@
1
+ <%= field_context[:body] %><a href="javascript:toggleEditor('<%= field_context[:id] %>');"><%= _"text.change.editor" %></a>
@@ -0,0 +1,3 @@
1
+ <%= render :file=>"#{TEMPLATE_PATH}/xhtml/controlheader.html.erb", :use_full_path=>false %>
2
+ <%= render :file=>"#{TEMPLATE_PATH}/simple/common.html.erb", :use_full_path=>false %>
3
+ <%= render :file=>"#{TEMPLATE_PATH}/xhtml/controlfooter.html.erb", :use_full_path=>false %>
@@ -0,0 +1,8 @@
1
+ </td>
2
+ </tr>
3
+ <% if field_context[:hidden][:desc] %>
4
+ <tr>
5
+ <td></td>
6
+ <td align="left"><%= _(("field.#{field_context[:simple_name]}.#{field_context[:method_name]}.desc"), :default => '') %></td>
7
+ </tr>
8
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <tr>
2
+ <td class="tdLabel" nowrap valign="top">
3
+ <label for="loginName" class="label">
4
+ <% if field_context[:hidden][:require] %><span class="requiredstar"><img src="/images/mustNeeded.gif" /></span><% end %>
5
+ <%= _("field.#{field_context[:simple_name]}.#{field_context[:method_name]}") %>:
6
+ </label>
7
+ </td>
8
+ <td
@@ -0,0 +1,3 @@
1
+ <%= render :file=>"#{TEMPLATE_PATH}/xhtml/controlheader.html.erb", :use_full_path=>false %>
2
+ <%= render :file=>"#{TEMPLATE_PATH}/simple_mce/text_area.html.erb", :use_full_path=>false %>
3
+ <%= render :file=>"#{TEMPLATE_PATH}/xhtml/controlfooter.html.erb", :use_full_path=>false %>
data/index.html ADDED
@@ -0,0 +1,51 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
+ <HTML>
3
+ <HEAD>
4
+ <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=gb2312">
5
+ <TITLE></TITLE>
6
+ <META NAME="GENERATOR" CONTENT="OpenOffice.org 2.2 (Win32)">
7
+ <META NAME="CREATED" CONTENT="20080617;9501531">
8
+ <META NAME="CHANGED" CONTENT="20080617;9511733">
9
+ <STYLE TYPE="text/css">
10
+ <!--
11
+ @page { size: 21cm 29.7cm; margin: 2cm }
12
+ P { margin-bottom: 0.21cm }
13
+ -->
14
+ </STYLE>
15
+ </HEAD>
16
+ <BODY LANG="zh-CN" DIR="LTR">
17
+ <P STYLE="margin-bottom: 0cm"><FONT FACE="Thorndale, serif"><SPAN LANG="en-US">Rails
18
+ Layout Extension, release 0.3.0 (Jun. 2008)<BR><BR>Feature<BR>=======<BR>1.
19
+ simplifying content_for usage by extracting key/value pair from
20
+ content_for data like YAML and setting them as content_for variables<BR>
21
+ </SPAN></FONT>��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">content_for</SPAN></FONT>��ʹ�ã���������<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">YAML</SPAN></FONT>��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">key/value</SPAN></FONT>��ʽ����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">content_for</SPAN></FONT>����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US"><BR>2.
22
+ let erb file can determine layout by changing the default behavior of
23
+ layout determining, just like java's sitemesh<BR>
24
+ </SPAN></FONT>ͨ���ı�Ĭ�ϵ�<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">layout</SPAN></FONT>��ȡ��ʽ��ʹ<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">erb</SPAN></FONT>Ҳ���Զ���<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">layout</SPAN></FONT>������<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">java</SPAN></FONT>��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">sitemesh<BR>3.
25
+ use controller method if helper method requested from erb not exists<BR>
26
+ ERB</SPAN></FONT>����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">helper</SPAN></FONT>�ķ��������ڣ����Զ�����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">controller</SPAN></FONT>�ķ���<FONT FACE="Thorndale, serif"><SPAN LANG="en-US"><BR>4.
27
+ support theme template for field tag<BR> </SPAN></FONT>֧��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">field
28
+ tag</SPAN></FONT>�������<FONT FACE="Thorndale, serif"><SPAN LANG="en-US"><BR><BR>Setup<BR>=======<BR>1.
29
+ rlayout as gem<BR>put the following line into your rails initializer,
30
+ or bottom of
31
+ environment.rb<BR></SPAN></FONT>���������д���ӵ�<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">rails</SPAN></FONT>��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">initializer</SPAN></FONT>�����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">environment.rb</SPAN></FONT>�����<FONT FACE="Thorndale, serif"><SPAN LANG="en-US"><BR>require
32
+ 'rlayout'<BR><BR>2. rlayout as plugin<BR>nothing need to
33
+ do<BR></SPAN></FONT>����Ҫ��ʲô��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US"><BR><BR>Usage<BR>=======<BR><BR>1.
34
+ simplifying content_for<BR><BR>&lt;% content_for :config do %&gt;<BR>title:
35
+ This is my title<BR>panel: This is my panel<BR>&lt;% end %&gt; <BR><BR>that
36
+ will do the same thing as the following:<BR><BR>&lt;%- content_for
37
+ :title do -%&gt;<BR>This is my title<BR>&lt;%- end -%&gt; <BR>&lt;%-
38
+ content_for :panel do -%&gt;<BR>This is my panel<BR>&lt;%- end -%&gt;
39
+ <BR><BR>2. determining layout in erb file<BR>&lt;% content_for
40
+ :config do %&gt;<BR>#note</SPAN></FONT>��<FONT FACE="Thorndale, serif"><SPAN LANG="en-US">key
41
+ layout won't be transferred to a content_for variable, it is only
42
+ used to determine a view layout<BR>layout: happy<BR>&lt;% end %&gt;
43
+ <BR><BR>3. use theme template of field tag<BR><BR>class
44
+ YourController<BR> form_theme :xhtml<BR>end<BR><BR>and see related
45
+ code in folder &quot;example/templates&quot;, the theme can be
46
+ defined in tag with :theme, that will override theme definition in
47
+ controller<BR><BR>Copyright (c) 2008 Leon Li, released under the MIT
48
+ license<BR></SPAN></FONT><BR>
49
+ </P>
50
+ </BODY>
51
+ </HTML>
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code as a rails plugin
2
+ require 'rlayout'
@@ -29,6 +29,7 @@ module Rlayout
29
29
  end
30
30
  end
31
31
  end
32
+ new_layout = params[:layout] if new_layout.nil? || new_layout.empty?
32
33
  unless new_layout.nil? || new_layout.empty?
33
34
  options ||= {}
34
35
  options[:layout] = new_layout == 'none' || new_layout == 'false' ? false : new_layout
@@ -0,0 +1,125 @@
1
+ =begin Rlayout
2
+ author: Leon Li(scorpio_leon@hotmail.com)
3
+ =end
4
+ #for field template
5
+
6
+ module Rlayout::Template
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+ module ClassMethods
11
+ def form_theme(theme)
12
+ write_inheritable_attribute "form_theme", theme
13
+ end
14
+
15
+ def get_form_theme
16
+ read_inheritable_attribute("form_theme")
17
+ end
18
+ end
19
+ end
20
+
21
+ require 'action_view/helpers/form_helper'
22
+ module ActionView::Helpers::FormHelper
23
+ def field_context
24
+ @field_context
25
+ end
26
+ def field_context=(context)
27
+ @field_context = context
28
+ end
29
+ end
30
+
31
+ class Rlayout::TemplateUtil
32
+ WITH_TEMPLATE = '_without_template'
33
+ class << self
34
+ def get_template(key, type=nil)
35
+ type = 'common' if type.nil?
36
+ template_key = key+'_'+type
37
+ @tag_templates ||= {}
38
+ if @tag_templates[template_key].nil? || ENV['RAILS_ENV'] != 'production'
39
+ template_file = File.join(RAILS_ROOT, 'templates', key, "#{type}.html.erb")
40
+ template_file = File.join(RAILS_ROOT, 'templates', key, "common.html.erb") unless File.exists?(template_file)
41
+ @tag_templates[template_key] = File.read(template_file)
42
+ end
43
+ @tag_templates[template_key]
44
+ end
45
+
46
+ def round_tag(tag_name, context, str, *args)
47
+ view_context = Thread.current[:rlayout][:view_context]
48
+ key = (context[:options] && context[:options].delete('theme')) || view_context.controller.form_theme
49
+ unless key.nil?
50
+ context[:body] = str
51
+ view_context.field_context=context
52
+ str = ERB.new(get_template(key, tag_name)).result(view_context.send(:binding))
53
+ view_context.field_context=nil
54
+ end
55
+ str
56
+ end
57
+ end
58
+ end
59
+
60
+ class ActionView::Helpers::InstanceTag
61
+
62
+ instance_methods.each do |m|
63
+ if m =~ /^to_(.*)_tag$/
64
+ next if $1 == 'content'
65
+ new_method = (m+Rlayout::TemplateUtil::WITH_TEMPLATE).to_sym
66
+ alias_method(new_method, m.to_sym) unless method_defined?(new_method)
67
+ class_eval %{
68
+ def #{m}(*args)
69
+ str = #{m}#{Rlayout::TemplateUtil::WITH_TEMPLATE}(*args)
70
+ context = {}
71
+ context[:input_type] = @tag_type_4template
72
+ context[:name] = @object_name
73
+ context[:simple_name] = @object_name.rindex('_').nil? ? @object_name : @object_name[(@object_name.rindex('_') + 1)..-1]
74
+ context[:full_name] = @tag_name_4template
75
+ context[:id] = @tag_id_4template
76
+ context[:object] = @object
77
+ context[:method_name] = @method_name
78
+ context[:options] = @options_4template
79
+ context[:hidden] = @hidden_options
80
+ Rlayout::TemplateUtil.round_tag('#{$1}', context, str, *args)
81
+ end
82
+ }
83
+ end
84
+ end
85
+
86
+ alias_method :add_default_name_and_id_old4template, :add_default_name_and_id unless method_defined?(:add_default_name_and_id_old4template)
87
+ def add_default_name_and_id(options)
88
+ add_default_name_and_id_old4template(options)
89
+ # if options['special_name']
90
+ # options['name'] = options['special_name'] == true ? @method_name : options['special_name']
91
+ # end
92
+ @tag_name_4template = options['name']
93
+ @tag_id_4template = options['id']
94
+ @tag_type_4template = options['type']
95
+ @options_4template = options
96
+ @hidden_options = options.delete('hidden') || {}
97
+ end
98
+
99
+ end
100
+ module ActionView::Helpers::FormTagHelper
101
+
102
+ instance_methods.each do |m|
103
+ if m =~ /(.*)_tag$/
104
+ next if $1 == 'form' || $1 == 'field_set'
105
+ new_method = (m+Rlayout::TemplateUtil::WITH_TEMPLATE).to_sym
106
+ alias_method(new_method, m.to_sym) unless method_defined?(new_method)
107
+ class_eval %{
108
+ def #{m}(*args)
109
+ options = args[-1]
110
+ name = #{['submit', 'image'].include?(m) ? '""' : 'args[0] ||= ""'}
111
+ context = {}
112
+ context[:name] = name
113
+ context[:simple_name] = name
114
+ context[:full_name] = name
115
+ context[:id] = name
116
+ context[:method_name] = name
117
+ context[:hidden] = options.delete('hidden') || {}
118
+ str = #{m}#{Rlayout::TemplateUtil::WITH_TEMPLATE}(*args)
119
+ context[:options] = options.stringify_keys!
120
+ Rlayout::TemplateUtil.round_tag('#{$1}', context, str, *args)
121
+ end
122
+ }
123
+ end
124
+ end
125
+ end
data/lib/rlayout.rb CHANGED
@@ -2,13 +2,37 @@
2
2
  author: Leon Li(scorpio_leon@hotmail.com)
3
3
  =end
4
4
  require 'rlayout/layout'
5
+ require 'rlayout/template'
5
6
  require 'action_view'
6
7
 
8
+
7
9
  ActionController::Base.class_eval do
10
+ #for page layout
8
11
  include Rlayout::Layout
12
+ include Rlayout::Template
13
+ #for field temlate
14
+ alias_method :process_cleanup_old4template, :process_cleanup
15
+ def process_cleanup
16
+ process_cleanup_old4template
17
+ Thread.current[:rlayout] = nil
18
+ end
19
+ def form_theme
20
+ self.class.get_form_theme
21
+ end
9
22
  end
10
23
 
24
+
11
25
  ActionView::Base.class_eval do
26
+
27
+ #for field temlate
28
+ alias_method :initialize_old4template, :initialize unless method_defined?(:initialize_old4template)
29
+ def initialize(*args)
30
+ initialize_old4template(*args)
31
+ Thread.current[:rlayout] ||= {}
32
+ Thread.current[:rlayout][:view_context] = self
33
+ end
34
+
35
+ #for page layout
12
36
  def method_missing(method_id, *args)
13
37
  ActionView::Base.class_eval %{
14
38
  def #{method_id}(*args)
@@ -17,4 +41,6 @@ ActionView::Base.class_eval do
17
41
  }
18
42
  send(method_id, *args)
19
43
  end
20
- end
44
+ end
45
+
46
+
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.2.1
4
+ version: 0.3.0
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-05-22 00:00:00 +08:00
12
+ date: 2008-06-17 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,9 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - CHANGELOG
26
+ - example
27
+ - index.html
28
+ - init.rb
26
29
  - lib
27
30
  - MIT-LICENSE
28
31
  - pkg
@@ -30,7 +33,19 @@ files:
30
33
  - README
31
34
  - lib/rlayout
32
35
  - lib/rlayout/layout.rb
36
+ - lib/rlayout/template.rb
33
37
  - lib/rlayout.rb
38
+ - example/templates
39
+ - example/templates/simple
40
+ - example/templates/simple/common.html.erb
41
+ - example/templates/simple_mce
42
+ - example/templates/simple_mce/text_area.html.erb
43
+ - example/templates/xhtml
44
+ - example/templates/xhtml/common.html.erb
45
+ - example/templates/xhtml/controlfooter.html.erb
46
+ - example/templates/xhtml/controlheader.html.erb
47
+ - example/templates/xhtml_mce
48
+ - example/templates/xhtml_mce/text_area.html.erb
34
49
  has_rdoc: false
35
50
  homepage: http://rlayout.rubyforge.org/
36
51
  post_install_message: