padrino-helpers 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -130,6 +130,9 @@ The list of defined helpers in the 'asset helpers' category:
130
130
  * <tt>link_to(*args, &block)</tt>
131
131
  * Creates a link element with given name, url and options
132
132
  * <tt>link_to 'click me', '/dashboard', :class => 'linky'</tt>
133
+ * <tt>link_to 'click me', '/dashboard', :class => 'linky', :if => @foo.present?</tt>
134
+ * <tt>link_to 'click me', '/dashboard', :class => 'linky', :unless => @foo.blank?</tt>
135
+ * <tt>link_to 'click me', '/dashboard', :class => 'linky', :unless => :current</tt>
133
136
  * <tt>link_to('/dashboard', :class => 'blocky') { ...content... }</tt>
134
137
  * <tt>mail_to(email, caption=nil, mail_options={})</tt>
135
138
  * Creates a mailto link tag to the specified email_address
@@ -179,7 +182,7 @@ The list of defined helpers in the 'form helpers' category:
179
182
  * <tt>field_set_tag("Office", :class => 'office-set') { }</tt>
180
183
  * <tt>error_messages_for(:record, options={})</tt>
181
184
  * Constructs list html for the errors for a given object
182
- * <tt>error_messages_for @user</tt>
185
+ * <tt>error_messages_for :user</tt>
183
186
  * <tt>label_tag(name, options={}, &block)</tt>
184
187
  * Constructs a label tag from the given options
185
188
  * <tt>label_tag :username, :class => 'long-label'</tt>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.7.1
@@ -2,8 +2,12 @@ module Padrino
2
2
  module Helpers
3
3
  module AssetTagHelpers
4
4
 
5
+ ##
5
6
  # Creates a div to display the flash of given type if it exists
6
- # flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
7
+ #
8
+ # Examples:
9
+ #
10
+ # flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
7
11
  def flash_tag(kind, options={})
8
12
  flash_text = flash[kind]
9
13
  return '' if flash_text.blank?
@@ -11,30 +15,46 @@ module Padrino
11
15
  content_tag(:div, flash_text, options)
12
16
  end
13
17
 
18
+ ##
14
19
  # Creates a link element with given name, url and options
15
- # link_to 'click me', '/dashboard', :class => 'linky'
16
- # link_to('/dashboard', :class => 'blocky') do ... end
17
- # parameters: name, url='javascript:void(0)', options={}, &block
20
+ #
21
+ # Examples:
22
+ #
23
+ # link_to 'click me', '/dashboard', :class => 'linky'
24
+ # link_to 'click me', '/dashboard', :if => @foo.present?
25
+ # link_to 'click me', '/dashboard', :unless => @foo.empty?
26
+ # link_to 'click me', '/dashboard', :unless => :current
27
+ # link_to('/dashboard', :class => 'blocky') do ... end
28
+ #
29
+ # Note that you can pass :+if+ or :+unless+ conditions, but if you provide :current as
30
+ # condition padrino return true/false if the request.path_info match the given url
31
+ #
18
32
  def link_to(*args, &block)
19
- options = args.extract_options!
33
+ options = args.extract_options!
20
34
  anchor = options[:anchor] ? "##{CGI.escape options.delete(:anchor).to_s}" : ""
21
35
  options["data-remote"] = "true" if options.delete(:remote)
22
36
  if block_given?
23
37
  url = args[0] || 'javascript:void(0);'
24
38
  options.reverse_merge!(:href => url + anchor)
25
39
  link_content = capture_html(&block)
40
+ return name unless parse_conditions(url, options)
26
41
  result_link = content_tag(:a, link_content, options)
27
42
  block_is_template?(block) ? concat_content(result_link) : result_link
28
43
  else
29
44
  name, url = args[0], (args[1] || 'javascript:void(0);')
45
+ return name unless parse_conditions(url, options)
30
46
  options.reverse_merge!(:href => url + anchor)
31
47
  content_tag(:a, name, options)
32
48
  end
33
49
  end
34
50
 
51
+ ##
35
52
  # Creates a mail link element with given name and caption
36
- # mail_to "me@demo.com" => <a href="mailto:me@demo.com">me@demo.com</a>
37
- # mail_to "me@demo.com", "My Email" => <a href="mailto:me@demo.com">My Email</a>
53
+ #
54
+ # Examples:
55
+ # mail_to "me@demo.com" => <a href="mailto:me@demo.com">me@demo.com</a>
56
+ # mail_to "me@demo.com", "My Email" => <a href="mailto:me@demo.com">My Email</a>
57
+ #
38
58
  def mail_to(email, caption=nil, mail_options={})
39
59
  html_options = mail_options.slice!(:cc, :bcc, :subject, :body)
40
60
  mail_query = Rack::Utils.build_query(mail_options).gsub(/\+/, '%20').gsub('%40', '@')
@@ -42,35 +62,67 @@ module Padrino
42
62
  link_to (caption || email), mail_href, html_options
43
63
  end
44
64
 
65
+ ##
45
66
  # Creates a meta element with the content and given options
46
- # meta_tag "weblog,news", :name => "keywords" => <meta name="keywords" content="weblog,news">
47
- # meta_tag "text/html; charset=UTF-8", :http-equiv => "Content-Type" => <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
67
+ #
68
+ # Examples:
69
+ #
70
+ # meta_tag "weblog,news", :name => "keywords" => <meta name="keywords" content="weblog,news">
71
+ # meta_tag "text/html; charset=UTF-8", :http-equiv => "Content-Type" => <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
72
+ #
48
73
  def meta_tag(content, options={})
49
74
  options.reverse_merge!("content" => content)
50
75
  tag(:meta, options)
51
76
  end
52
77
 
78
+ ##
53
79
  # Creates an image element with given url and options
54
- # image_tag('icons/avatar.png')
80
+ #
81
+ # Example:
82
+ #
83
+ # image_tag('icons/avatar.png')
84
+ #
55
85
  def image_tag(url, options={})
56
86
  options.reverse_merge!(:src => image_path(url))
57
87
  tag(:img, options)
58
88
  end
59
89
 
60
- # Returns a stylesheet link tag for the sources specified as arguments
61
- # stylesheet_link_tag 'style', 'application', 'layout'
90
+ ##
91
+ # Returns an html script tag for each of the sources provided.
92
+ # You can pass in the filename without extension or a symbol and we search it in your +appname.public+
93
+ # like app/public/stylesheets for inclusion. You can provide also a full path.
94
+ #
95
+ # Example:
96
+ #
97
+ # stylesheet_link_tag 'style', 'application', 'layout'
98
+ #
62
99
  def stylesheet_link_tag(*sources)
63
100
  options = sources.extract_options!.symbolize_keys
64
101
  sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
65
102
  end
66
103
 
67
- # javascript_include_tag 'application', 'special'
104
+ ##
105
+ # Returns an html script tag for each of the sources provided.
106
+ # You can pass in the filename without extension or a symbol and we search it in your +appname.public+
107
+ # like app/public/javascript for inclusion. You can provide also a full path.
108
+ #
109
+ # Example:
110
+ #
111
+ # javascript_include_tag 'application', :extjs
112
+ #
68
113
  def javascript_include_tag(*sources)
69
114
  options = sources.extract_options!.symbolize_keys
70
115
  sources.collect { |script| javascript_tag(script, options) }.join("\n")
71
116
  end
72
117
 
73
- # Returns the path to the image, either relative or absolute
118
+ ##
119
+ # Returns the path to the image, either relative or absolute. We search it in your +appname.public+
120
+ # like app/public/images for inclusion. You can provide also a full path.
121
+ #
122
+ # Example:
123
+ #
124
+ # image_path("foo.jpg") => "yourapp/public/images/foo.jpg"
125
+ #
74
126
  def image_path(src)
75
127
  src.gsub!(/\s/, '')
76
128
  src =~ %r{^\s*(/|http)} ? src : uri_root_path('images', src)
@@ -96,31 +148,42 @@ module Padrino
96
148
  source_name = source; source_name << ".js" unless source =~ /\.js/
97
149
  result_path = source_name if source =~ %r{^/} # absolute path
98
150
  result_path ||= uri_root_path("javascripts", source_name)
99
- if result_path =~ /\?/
100
- result_path
101
- else
102
- stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
103
- "#{result_path}?#{stamp}"
104
- end
151
+ return result_path if result_path =~ /\?/
152
+ public_path = Padrino.root("public", result_path)
153
+ stamp = File.exist?(public_path) ? File.mtime(public_path).to_i : Time.now.to_i
154
+ "#{result_path}?#{stamp}"
105
155
  end
106
156
 
107
157
  # Returns the stylesheet_path appending the default stylesheets path if necessary
108
158
  def stylesheet_path(source)
109
159
  return source if source =~ /^http/
110
- source = source.to_s.gsub(/\.css$/, '')
111
- source_name = source; source_name << ".css" unless source =~ /\.css/
112
- result_path = source_name if source =~ %r{^/} # absolute path
160
+ source = source.to_s.gsub(/\.css$/, '')
161
+ source_name = source; source_name << ".css" unless source =~ /\.css/
162
+ result_path = source_name if source =~ %r{^/} # absolute path
113
163
  result_path ||= uri_root_path("stylesheets", source_name)
114
- stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
164
+ return result_path if result_path =~ /\?/
165
+ public_path = Padrino.root("public", result_path)
166
+ stamp = File.exist?(public_path) ? File.mtime(public_path).to_i : Time.now.to_i
115
167
  "#{result_path}?#{stamp}"
116
168
  end
117
169
 
118
170
  # Returns the uri root of the application, defaulting to '/'
119
- # @example uri_root('javascripts')
120
171
  def uri_root_path(*paths)
121
172
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
122
173
  File.join(root_uri || '/', *paths)
123
174
  end
175
+
176
+ # Parse link_to options for give correct conditions
177
+ def parse_conditions(url, options)
178
+ if options.has_key?(:if)
179
+ condition = options.delete(:if)
180
+ condition == :current ? url == request.path_info : condition
181
+ elsif condition = options.delete(:unless)
182
+ condition == :current ? url != request.path_info : !condition
183
+ else
184
+ true
185
+ end
186
+ end
124
187
  end
125
188
  end
126
189
  end
@@ -13,7 +13,7 @@ module Padrino
13
13
 
14
14
  # f.error_messages
15
15
  def error_messages(*params)
16
- params.unshift object_name.to_sym
16
+ params.unshift object
17
17
  @template.error_messages_for(*params)
18
18
  end
19
19
 
@@ -137,7 +137,7 @@ module Padrino
137
137
  # explicit_object is either a symbol or a record
138
138
  # Returns a new record of the type specified in the object
139
139
  def build_object(object_or_symbol)
140
- object_or_symbol.is_a?(Symbol) ? object_class(object_or_symbol).new : object_or_symbol
140
+ object_or_symbol.is_a?(Symbol) ? @template.instance_variable_get("@#{object_or_symbol}") || object_class(object_or_symbol).new : object_or_symbol
141
141
  end
142
142
 
143
143
  # Returns the class type for the given object
@@ -216,7 +216,7 @@ module Padrino
216
216
  # If explicitly defined, returns that, otherwise returns defaults
217
217
  # configured_form_builder_class(nil) => StandardFormBuilder
218
218
  def configured_form_builder_class(explicit_builder=nil)
219
- default_builder = self.respond_to?(:options) && self.options.default_builder
219
+ default_builder = self.respond_to?(:options) && self.options.default_builder
220
220
  configured_builder = explicit_builder || default_builder || 'StandardFormBuilder'
221
221
  configured_builder = "Padrino::Helpers::FormBuilder::#{configured_builder}".constantize if configured_builder.is_a?(String)
222
222
  configured_builder
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-helpers}
8
- s.version = "0.7.0"
8
+ s.version = "0.7.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2010-01-31}
12
+ s.date = %q{2010-02-03}
13
13
  s.description = %q{Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino}
14
14
  s.email = %q{nesquena@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -80,7 +80,7 @@ Gem::Specification.new do |s|
80
80
 
81
81
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
82
82
  s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
83
- s.add_runtime_dependency(%q<padrino-core>, ["= 0.7.0"])
83
+ s.add_runtime_dependency(%q<padrino-core>, ["= 0.7.1"])
84
84
  s.add_development_dependency(%q<haml>, [">= 2.2.1"])
85
85
  s.add_development_dependency(%q<shoulda>, [">= 0"])
86
86
  s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
@@ -88,7 +88,7 @@ Gem::Specification.new do |s|
88
88
  s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
89
89
  else
90
90
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
91
- s.add_dependency(%q<padrino-core>, ["= 0.7.0"])
91
+ s.add_dependency(%q<padrino-core>, ["= 0.7.1"])
92
92
  s.add_dependency(%q<haml>, [">= 2.2.1"])
93
93
  s.add_dependency(%q<shoulda>, [">= 0"])
94
94
  s.add_dependency(%q<mocha>, [">= 0.9.7"])
@@ -97,7 +97,7 @@ Gem::Specification.new do |s|
97
97
  end
98
98
  else
99
99
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
100
- s.add_dependency(%q<padrino-core>, ["= 0.7.0"])
100
+ s.add_dependency(%q<padrino-core>, ["= 0.7.1"])
101
101
  s.add_dependency(%q<haml>, [">= 2.2.1"])
102
102
  s.add_dependency(%q<shoulda>, [">= 0"])
103
103
  s.add_dependency(%q<mocha>, [">= 0.9.7"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2010-01-31 00:00:00 +01:00
15
+ date: 2010-02-03 00:00:00 +01:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  requirements:
34
34
  - - "="
35
35
  - !ruby/object:Gem::Version
36
- version: 0.7.0
36
+ version: 0.7.1
37
37
  version:
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: haml