handicraft_helper 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +0 -0
- data/README +15 -0
- data/init.rb +5 -0
- data/lib/handicraft/form.rb +70 -0
- data/lib/handicraft/helper.rb +175 -0
- metadata +71 -0
data/LICENSE
ADDED
File without changes
|
data/README
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Author:: Wen-Tien Chang(mailto:ihower@handlino.com)
|
2
|
+
Copyright:: Copyright (c) 2009 Handlino Inc.
|
3
|
+
Licensed under the MIT: http://www.opensource.org/licenses/mit-license.php
|
4
|
+
|
5
|
+
handicraft_helper is some lovely Rails helper
|
6
|
+
|
7
|
+
USAGE:
|
8
|
+
|
9
|
+
in your application_controller.rb
|
10
|
+
|
11
|
+
class ApplicationController < ActionController::Base
|
12
|
+
|
13
|
+
helper Handicraft::Helper
|
14
|
+
|
15
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Handicraft
|
2
|
+
|
3
|
+
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag }
|
4
|
+
|
5
|
+
class Form < ActionView::Helpers::FormBuilder
|
6
|
+
helpers = field_helpers +
|
7
|
+
%w(date_select datetime_select time_select radio_button) +
|
8
|
+
%w(collection_select select country_select time_zone_select) -
|
9
|
+
%w(hidden_field label fields_for)
|
10
|
+
|
11
|
+
helpers.each do |name|
|
12
|
+
define_method(name) do |field, *args|
|
13
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
14
|
+
|
15
|
+
li = ( error_message_on(field).blank? )? Handicraft::Helper::TagNode.new("li") : Handicraft::Helper::TagNode.new("li", :class => 'error' )
|
16
|
+
li << label(field, options.delete(:label) , :class => "desc" ) if options[:label]
|
17
|
+
|
18
|
+
div = Handicraft::Helper::TagNode.new("div", :class=> "col")
|
19
|
+
|
20
|
+
options[:class] = case name
|
21
|
+
when "text_field" : 'field text'
|
22
|
+
when "text_area" : 'field textarea'
|
23
|
+
#when "select" : 'field select'
|
24
|
+
#when "date_select" : 'field select'
|
25
|
+
#when "datetime_select" : 'field select'
|
26
|
+
#when "time_select" : 'field select'
|
27
|
+
#when "collection_select" : 'field select'
|
28
|
+
#when "country_select" : 'field select'
|
29
|
+
#when "time_zone_select" : 'field select'
|
30
|
+
end
|
31
|
+
|
32
|
+
args << options
|
33
|
+
div << super(field, *args)
|
34
|
+
|
35
|
+
unless error_message_on(field).blank?
|
36
|
+
error_msg = error_message_on(field).match(/<div class=\"formError\">(.*)<\/div>/)[1] # HACK!
|
37
|
+
div << @template.content_tag("p", error_msg, :class => 'error')
|
38
|
+
end
|
39
|
+
|
40
|
+
div << @template.content_tag("p", options[:description], :class => "instruction" ) if options[:description]
|
41
|
+
|
42
|
+
li << div
|
43
|
+
return li.to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def many_check_boxes(name, check_boxes_options, options = {})
|
48
|
+
li = Handicraft::Helper::TagNode.new("li")
|
49
|
+
li << label(name, options.delete(:label) , :class => "desc" ) if options[:label]
|
50
|
+
|
51
|
+
div = Handicraft::Helper::TagNode.new("div", :class=> "col")
|
52
|
+
|
53
|
+
field_name = "#{object_name}[#{name}][]"
|
54
|
+
check_boxes_options.each do |value, key|
|
55
|
+
div << @template.check_box_tag(field_name, key, object.send(name).include?(key)) + " #{value} "
|
56
|
+
end
|
57
|
+
|
58
|
+
div << @template.hidden_field_tag(field_name, "")
|
59
|
+
li << div
|
60
|
+
|
61
|
+
return li.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
def submit(value, options={})
|
65
|
+
options[:class] ||= 'submit'
|
66
|
+
|
67
|
+
@template.content_tag(:li, super(value, options), :class => "buttons")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
module Handicraft
|
2
|
+
module Helper
|
3
|
+
def yield_or_default(message, default_message = "")
|
4
|
+
message.nil? ? default_message : message
|
5
|
+
end
|
6
|
+
|
7
|
+
def handicraft_form_for(name, *args, &block)
|
8
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
9
|
+
options = options.merge(:builder => Handicraft::Form)
|
10
|
+
|
11
|
+
if options[:html] && options[:html][:class].nil?
|
12
|
+
options[:html].merge!( :class => "form right-label" )
|
13
|
+
elsif options[:html].nil?
|
14
|
+
options[:html] = { :class => "form right-label" }
|
15
|
+
end
|
16
|
+
|
17
|
+
args = (args << options)
|
18
|
+
form_for(name, *args, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def breadcrumb( *crumb )
|
22
|
+
p = TagNode.new( :p, :class => "breadcrumb" )
|
23
|
+
p << crumb.join(' > ')
|
24
|
+
return p.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_page_title
|
28
|
+
title = @page_title ? "#{SITE_NAME} | #{@page_title}" : SITE_NAME rescue "SITE_NAME"
|
29
|
+
content_tag("title", title)
|
30
|
+
end
|
31
|
+
|
32
|
+
def render_body_tag
|
33
|
+
class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
|
34
|
+
id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
|
35
|
+
|
36
|
+
%Q|<!--[if lt IE 7 ]>
|
37
|
+
<body class="#{class_attribute} ie6"><![endif]-->
|
38
|
+
<!--[if gte IE 7 ]>
|
39
|
+
<body class="#{class_attribute} ie"><![endif]-->
|
40
|
+
<!--[if !IE]>-->
|
41
|
+
<body#{id_attribute} class="#{class_attribute}">
|
42
|
+
<!--<![endif]-->|
|
43
|
+
end
|
44
|
+
|
45
|
+
def s(html)
|
46
|
+
sanitize( html, :tags => %w(table thead tbody tr td th ol ul li div span font img sup sub br hr a pre p h1 h2 h3 h4 h5 h6), :attributes => %w(id class style src href size color) )
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_table(rows, renderrers, table_options = {})
|
50
|
+
table_options = {
|
51
|
+
:has_header => true,
|
52
|
+
:has_row_info => false,
|
53
|
+
:id => nil,
|
54
|
+
:class_name => "auto"
|
55
|
+
}.merge(table_options)
|
56
|
+
|
57
|
+
table_tag_options = table_options[:id] ? { :id => table_options[:id], :class => table_options[:class_name] } : { :class => table_options[:class_name] }
|
58
|
+
|
59
|
+
table = TagNode.new('table', table_tag_options)
|
60
|
+
if table_options[:has_header] == true
|
61
|
+
table << thead = TagNode.new(:thead)
|
62
|
+
thead << tr = TagNode.new(:tr, :class => 'odd')
|
63
|
+
|
64
|
+
renderrers.each do |renderrer|
|
65
|
+
tr << th = TagNode.new(:th)
|
66
|
+
th << renderrer[0]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
table << tbody = TagNode.new('tbody')
|
71
|
+
row_info = {}
|
72
|
+
row_info[:total] = rows.length
|
73
|
+
rows.each_with_index do |row,i|
|
74
|
+
row_info[:current] = i
|
75
|
+
tbody << tr = TagNode.new('tr', :class => cycle("","odd") )
|
76
|
+
renderrers.each do |renderrer|
|
77
|
+
tr << td = TagNode.new('td')
|
78
|
+
|
79
|
+
if renderrer[1].class == Proc
|
80
|
+
if table_options[:has_row_info] == true
|
81
|
+
td << renderrer[1].call(row, row_info)
|
82
|
+
else
|
83
|
+
td << renderrer[1].call(row)
|
84
|
+
end
|
85
|
+
else
|
86
|
+
td << renderrer[1]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
return table.to_s
|
92
|
+
end
|
93
|
+
|
94
|
+
# .current will be added to current action, but if you want to add .current to another link, you can set @current = ['/other_link']
|
95
|
+
# TODO: hot about render_list( *args )
|
96
|
+
def render_list(list=[], options={})
|
97
|
+
if list.is_a? Hash
|
98
|
+
options = list
|
99
|
+
list = []
|
100
|
+
end
|
101
|
+
|
102
|
+
yield(list) if block_given?
|
103
|
+
|
104
|
+
ul = TagNode.new('ul', :id => options[:id], :class => options[:class] )
|
105
|
+
|
106
|
+
list.each_with_index do |content, i|
|
107
|
+
item_class = []
|
108
|
+
item_class << "first" if i == 0
|
109
|
+
item_class << "last" if i == (list.length - 1)
|
110
|
+
|
111
|
+
item_content = content
|
112
|
+
item_options = {}
|
113
|
+
|
114
|
+
if content.is_a? Array
|
115
|
+
item_content = content[0]
|
116
|
+
item_options = content[1]
|
117
|
+
end
|
118
|
+
|
119
|
+
if item_options[:class]
|
120
|
+
item_class << item_options[:class]
|
121
|
+
end
|
122
|
+
|
123
|
+
link = item_content.match(/href=(["'])(.*?)(\1)/)[2] rescue nil
|
124
|
+
|
125
|
+
if ( link && current_page?(link) ) || ( @current && @current.include?(link) )
|
126
|
+
item_class << "current"
|
127
|
+
end
|
128
|
+
|
129
|
+
item_class = (item_class.empty?)? nil : item_class.join(" ")
|
130
|
+
ul << li = TagNode.new('li', :class => item_class )
|
131
|
+
li << item_content
|
132
|
+
end
|
133
|
+
|
134
|
+
return ul.to_s
|
135
|
+
end
|
136
|
+
|
137
|
+
# Composite pattern
|
138
|
+
class TagNode
|
139
|
+
include ActionView::Helpers::TagHelper
|
140
|
+
|
141
|
+
def initialize(name, options = {})
|
142
|
+
@name = name.to_s
|
143
|
+
@attributes = options
|
144
|
+
@children = []
|
145
|
+
end
|
146
|
+
|
147
|
+
def to_s
|
148
|
+
value = @children.each { |c| c.to_s }.join
|
149
|
+
content_tag(@name, value.to_s, @attributes)
|
150
|
+
end
|
151
|
+
|
152
|
+
def <<(tag_node)
|
153
|
+
@children << tag_node
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
module ControllerHelper
|
159
|
+
def set_breadcrumbs
|
160
|
+
@breadcrumbs = ["<a href='/'>首頁</a>"]
|
161
|
+
end
|
162
|
+
|
163
|
+
def drop_breadcrumb(title=nil, url=nil)
|
164
|
+
title ||= @page_title
|
165
|
+
url ||= url_for
|
166
|
+
if title
|
167
|
+
@breadcrumbs.push("<a href=\"#{url}\">#{title}</a>")
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def no_breadcrumbs
|
172
|
+
@breadcrumbs = []
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: handicraft_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 13
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: "1.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Handlino Inc.
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-07 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Handicraft Helper
|
22
|
+
email:
|
23
|
+
- dev@handlino.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/handicraft/form.rb
|
32
|
+
- lib/handicraft/helper.rb
|
33
|
+
- LICENSE
|
34
|
+
- README
|
35
|
+
- init.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://handlino.com
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Handicraft Helper
|
70
|
+
test_files: []
|
71
|
+
|