merb_helpers 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -13,27 +13,4 @@ dependency "merb_helpers"
13
13
 
14
14
  #...
15
15
 
16
- By default all modules of merb_helpers are loaded and are available to your merb app.
17
-
18
- You can selectively include/exclude modules from this list to keep you app lean if you like.
19
- The inclusions/exclusions are relative to all modules.
20
-
21
- \:include: will only include the specified modules.
22
-
23
- \:exclude: will include all except the specified modules.
24
-
25
- Do not use :include: and :exclude: options at the same time or an error will be raised.
26
-
27
- To set this up in config/plugins.yml
28
-
29
- To Include specified helpers
30
-
31
- \:merb_helpers:
32
- \:include: - date_time_helpers
33
- - form_helpers
34
-
35
- To Exclude specified helpers
36
-
37
- \:merb_helpers:
38
- \:exclude: - date_time_helpers
39
- - form_helpers
16
+ # TODO: describe date_time_helpers, form_helpers, tag_helpers
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'spec/rake/spectask'
5
5
 
6
6
  PLUGIN = "merb_helpers"
7
7
  NAME = "merb_helpers"
8
- VERSION = "0.9.2"
8
+ VERSION = "0.9.3"
9
9
  AUTHOR = "Yehuda Katz"
10
10
  EMAIL = "wycats@gmail.com"
11
11
  HOMEPAGE = "http://merb.rubyforge.org/"
@@ -26,7 +26,7 @@ spec = Gem::Specification.new do |s|
26
26
  s.author = AUTHOR
27
27
  s.email = EMAIL
28
28
  s.homepage = HOMEPAGE
29
- s.add_dependency("merb-core", ">=0.9.2")
29
+ s.add_dependency("merb-core", ">=0.9.3")
30
30
  s.require_path = 'lib'
31
31
  s.autorequire = PLUGIN
32
32
  s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
@@ -1,68 +1,22 @@
1
- module TimeDSL
2
- {:second => 1,
3
- :minute => 60,
4
- :hour => 3600,
5
- :day => [24,:hours],
6
- :week => [7,:days],
7
- :month => [30,:days],
8
- :year => [364.25, :days]}.each do |meth, amount|
9
- define_method meth do
10
- amount = amount.is_a?(Array) ? amount[0].send(amount[1]) : amount
11
- self * amount
12
- end
13
- alias_method "#{meth}s".intern, meth
14
- end
15
-
16
- # Reads best without arguments: 10.minutes.ago
17
- def ago(time = ::Time.now)
18
- time - self
19
- end
20
- alias :until :ago
21
-
22
- # Reads best with argument: 10.minutes.since(time)
23
- def since(time = ::Time.now)
24
- time + self
25
- end
26
- alias :from_now :since
27
- end
28
1
 
29
- class Integer
30
- # Ordinalize turns a number into an ordinal string used to denote the
31
- # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
32
- #
33
- # Examples
34
- # 1.ordinalize # => "1st"
35
- # 2.ordinalize # => "2nd"
36
- # 1002.ordinalize # => "1002nd"
37
- # 1003.ordinalize # => "1003rd"
38
- def ordinalize
39
- if (11..13).include?(self % 100)
40
- "#{self}th"
41
- else
42
- case self % 10
43
- when 1; "#{self}st"
44
- when 2; "#{self}nd"
45
- when 3; "#{self}rd"
46
- else "#{self}th"
47
- end
48
- end
49
- end
50
- end
51
-
52
- Numeric.send :include, TimeDSL
53
2
  # Everything above here has pretty much been implemented in the assistance gem...
54
3
 
55
4
  # Time.now.to_ordinalized_s :long
56
5
  # => "February 28th, 2006 21:10"
57
6
  module OrdinalizedFormatting
7
+
8
+ def self.extended(obj)
9
+ include Merb::Helpers::DateAndTime
10
+ end
11
+
58
12
  def to_ordinalized_s(format = :default)
59
- format = Merb::Helpers::DateAndTime::DATE_FORMATS[format]
60
- return to_default_s if format.nil?
13
+ format = Merb::Helpers::DateAndTime.date_formats[format]
14
+ return self.to_s if format.nil?
61
15
  strftime_ordinalized(format)
62
16
  end
63
17
 
64
18
  def strftime_ordinalized(fmt)
65
- strftime(fmt.gsub(/%d/, '_%d_')).gsub(/_(\d+)_/) { |s| s.to_i.ordinalize }
19
+ strftime(fmt.gsub(/(^|[^-])%d/, '\1_%d_')).gsub(/_(\d+)_/) { |s| s.to_i.ordinalize }
66
20
  end
67
21
  end
68
22
 
@@ -103,15 +57,15 @@ module Merb
103
57
  # The key methods are `relative_date`, `relative_date_span`, and `relative_time_span`. This also gives
104
58
  # you the Rails style Time DSL for working with numbers eg. 3.months.ago or 5.days.until(1.year.from_now)
105
59
  module DateAndTime
106
- TIME_CLASS = Time
107
- TIME_OUTPUT = {
60
+ @@time_class = Time
61
+ @@time_output = {
108
62
  :today => 'today',
109
63
  :yesterday => 'yesterday',
110
64
  :tomorrow => 'tomorrow',
111
65
  :initial_format => '%b %d',
112
66
  :year_format => ', %Y'
113
67
  }
114
- DATE_FORMATS = {
68
+ @@date_formats = {
115
69
  :db => "%Y-%m-%d %H:%M:%S",
116
70
  :time => "%H:%M",
117
71
  :short => "%d %b %H:%M",
@@ -120,6 +74,18 @@ module Merb
120
74
  :rfc822 => "%a, %d %b %Y %H:%M:%S %z"
121
75
  }
122
76
 
77
+ def self.time_class
78
+ @@time_class
79
+ end
80
+
81
+ def time_output
82
+ @@time_output
83
+ end
84
+
85
+ def date_formats
86
+ @@date_formats
87
+ end
88
+
123
89
  # Gives you a relative date in an attractive format
124
90
  #
125
91
  # ==== Parameters
@@ -134,16 +100,16 @@ module Merb
134
100
  # relative_date(1.year.ago) => "March 10th, 2007"
135
101
  def relative_date(time)
136
102
  date = time.to_date
137
- today = TIME_CLASS.now.to_date
103
+ today = DateAndTime.time_class.now.to_date
138
104
  if date == today
139
- TIME_OUTPUT[:today]
105
+ DateAndTime.time_output[:today]
140
106
  elsif date == (today - 1)
141
- TIME_OUTPUT[:yesterday]
107
+ DateAndTime.time_output[:yesterday]
142
108
  elsif date == (today + 1)
143
- TIME_OUTPUT[:tomorrow]
109
+ DateAndTime.time_output[:tomorrow]
144
110
  else
145
- fmt = TIME_OUTPUT[:initial_format].dup
146
- fmt << TIME_OUTPUT[:year_format] unless date.year == today.year
111
+ fmt = DateAndTime.time_output[:initial_format].dup
112
+ fmt << DateAndTime.time_output[:year_format] unless date.year == today.year
147
113
  time.strftime_ordinalized(fmt)
148
114
  end
149
115
  end
@@ -167,7 +133,7 @@ module Merb
167
133
  if times.first == times.last
168
134
  relative_date(times.first)
169
135
  else
170
- first = times.first; last = times.last; now = TIME_CLASS.now
136
+ first = times.first; last = times.last; now = DateAndTime.time_class.now
171
137
  arr = [first.strftime_ordinalized('%b %d')]
172
138
  arr << ", #{first.year}" unless first.year == last.year
173
139
  arr << ' - '
@@ -201,7 +167,7 @@ module Merb
201
167
  "#{prettier_time(times.first, !same_half)} - #{prettier_time(times.last)} #{relative_date(times.first)}"
202
168
 
203
169
  else
204
- first = times.first; last = times.last; now = TIME_CLASS.now
170
+ first = times.first; last = times.last; now = DateAndTime.time_class.now
205
171
  arr = [prettier_time(first)]
206
172
  arr << ' '
207
173
  arr << first.strftime_ordinalized('%b %d')
@@ -273,4 +239,4 @@ end
273
239
 
274
240
  class Merb::Controller #:nodoc:
275
241
  include Merb::Helpers::DateAndTime
276
- end
242
+ end
@@ -54,25 +54,34 @@ module Merb #:nodoc:
54
54
  # <%= error_messages_for :person, lambda{|error| "<li class='aieeee'>#{error.join(' ')}"} %>
55
55
  # <%= error_messages_for :person, nil, 'bad_mojo' %>
56
56
  def error_messages_for(obj, build_li = nil, html_class='error')
57
+ obj = self.instance_variable_get("@#{obj}") if obj.kind_of?(Symbol)
58
+
57
59
  return "" unless obj.respond_to?(:errors) && ! obj.errors.empty?
60
+
61
+ if obj.errors.respond_to?(:each) # AR & DM
62
+ build_li ||= lambda{|err| "<li>#{err.join(' ')}</li>"}
63
+ error_collection = obj.errors
64
+ else # Sequel
65
+ build_li ||= lambda{|msg| "<li>#{msg}</li>"}
66
+ error_collection = obj.errors.full_messages
67
+ end
68
+ error_count = error_collection.size
58
69
 
59
70
  header_message = if block_given?
60
71
  yield(obj.errors)
61
72
  else
62
- error_plurality = (obj.errors.size == 1 ? 'problem' : 'problems')
63
- "<h2>Form submittal failed because of #{obj.errors.size} #{error_plurality}</h2>"
73
+ error_plurality = (error_count == 1 ? 'problem' : 'problems')
74
+ "<h2>Form submittal failed because of #{error_count} #{error_plurality}</h2>"
64
75
  end
65
76
 
66
- build_li ||= lambda{|err| "<li>#{err.join(' ')}</li>"}
67
-
68
77
  markup = %Q{
69
78
  <div class='#{html_class}'>
70
79
  #{header_message}
71
80
  <ul>
72
81
  }
73
82
 
74
- obj.errors.each {|error| markup << build_li.call(error) }
75
-
83
+ error_collection.each {|error, message| markup << build_li.call([error, message])}
84
+
76
85
  markup << %Q{
77
86
  </ul>
78
87
  </div>
@@ -232,9 +241,11 @@ module Merb #:nodoc:
232
241
  # <%= checkbox_control :is_activated, :label => "Activated?" %>
233
242
  def checkbox_control(col, attrs = {}, hidden_attrs={})
234
243
  errorify_field(attrs, col)
235
- attrs.merge!(:checked => "checked") if col_val_to_bool(@_obj.send(col))
244
+ method_name = @_obj.respond_to?(col) ? col : :"#{col}?"
245
+ attrs.merge!(:checked => "checked") if col_val_to_bool(@_obj.send(method_name))
236
246
  attrs.merge!(:id => control_id(col))
237
- checkbox_field(control_name_value(col, attrs), hidden_attrs)
247
+ attrs = {:name => control_name(col), :value => control_value(method_name)}.merge(attrs)
248
+ checkbox_field(attrs, hidden_attrs)
238
249
  end
239
250
 
240
251
  # Provides a generic HTML checkbox input tag.
@@ -440,7 +451,7 @@ module Merb #:nodoc:
440
451
  end
441
452
  else
442
453
  collection.each do |value,text|
443
- options = selected.to_a.include?(value) ? {:selected => 'selected'} : {}
454
+ options = Array(selected).include?(value) ? {:selected => 'selected'} : {}
444
455
  ret << tag( 'option', text, {:value => value}.merge(options) )
445
456
  end
446
457
  end
@@ -0,0 +1,23 @@
1
+ module Ordinalize
2
+ # Ordinalize turns a number into an ordinal string used to denote the
3
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
4
+ #
5
+ # Examples
6
+ # 1.ordinalize # => "1st"
7
+ # 2.ordinalize # => "2nd"
8
+ # 1002.ordinalize # => "1002nd"
9
+ # 1003.ordinalize # => "1003rd"
10
+ def ordinalize
11
+ if (11..13).include?(self % 100)
12
+ "#{self}th"
13
+ else
14
+ case self % 10
15
+ when 1; "#{self}st"
16
+ when 2; "#{self}nd"
17
+ when 3; "#{self}rd"
18
+ else "#{self}th"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ Integer.send :include, Ordinalize
@@ -0,0 +1,58 @@
1
+ # Provides a a simple way of calling time units and to see the elapsed time between 2 moments
2
+ # ==== Examples
3
+ # 142.minutes => returns a value in seconds
4
+ # 7.days => returns a value in seconds
5
+ # 1.week => returns a value in seconds
6
+ # 2.weeks.ago => returns a date
7
+ # 1.year.since(time) => returns a date
8
+ # 5.months.since(2.weeks.from_now) => returns a date
9
+ module TimeDSL
10
+
11
+ def second
12
+ self * 1
13
+ end
14
+ alias_method :seconds, :second
15
+
16
+ def minute
17
+ self * 60
18
+ end
19
+ alias_method :minutes, :minute
20
+
21
+ def hour
22
+ self * 3600
23
+ end
24
+ alias_method :hours, :hour
25
+
26
+ def day
27
+ self * 86400
28
+ end
29
+ alias_method :days, :day
30
+
31
+ def week
32
+ self * 604800
33
+ end
34
+ alias_method :weeks, :week
35
+
36
+ def month
37
+ self * 2592000
38
+ end
39
+ alias_method :months, :month
40
+
41
+ def year
42
+ self * 31471200
43
+ end
44
+ alias_method :years, :year
45
+
46
+ # Reads best without arguments: 10.minutes.ago
47
+ def ago(time = ::Time.now)
48
+ time - self
49
+ end
50
+ alias :until :ago
51
+
52
+ # Reads best with argument: 10.minutes.since(time)
53
+ def since(time = ::Time.now)
54
+ time + self
55
+ end
56
+ alias :from_now :since
57
+ end
58
+ Numeric.send :include, TimeDSL
data/lib/merb_helpers.rb CHANGED
@@ -9,6 +9,13 @@ module Merb
9
9
  end
10
10
 
11
11
  def self.load
12
+ require HELPERS_DIR + '/time_dsl'
13
+ require HELPERS_DIR + '/ordinalize'
14
+ # TODO remove with and without and only allow 2 options:
15
+ # config[:load]
16
+ # if defined then load the modules passed along
17
+ # otherwise load everything
18
+
12
19
  if Merb::Plugins.config[:merb_helpers]
13
20
  config = Merb::Plugins.config[:merb_helpers]
14
21
  raise "With and Without options cannot be used with merb_helpers plugin configuration" if config[:with] && config[:without]
@@ -31,5 +38,5 @@ module Merb
31
38
  end
32
39
 
33
40
  Merb::BootLoader.before_app_loads do
34
- Merb::Helpers.load if defined?(Merb::Plugins)
35
- end
41
+ Merb::Helpers.load
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -9,7 +9,7 @@ autorequire: merb_helpers
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-24 00:00:00 -05:00
12
+ date: 2008-05-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.9.2
22
+ version: 0.9.3
23
23
  version:
24
24
  description: Helper support for merb (similar to the Rails form helpers)
25
25
  email: wycats@gmail.com
@@ -39,8 +39,9 @@ files:
39
39
  - lib/merb_helpers
40
40
  - lib/merb_helpers/date_time_helpers.rb
41
41
  - lib/merb_helpers/form_helpers.rb
42
- - lib/merb_helpers/form_model.rb
42
+ - lib/merb_helpers/ordinalize.rb
43
43
  - lib/merb_helpers/tag_helpers.rb
44
+ - lib/merb_helpers/time_dsl.rb
44
45
  - lib/merb_helpers.rb
45
46
  has_rdoc: true
46
47
  homepage: http://merb.rubyforge.org/
@@ -1,5 +0,0 @@
1
- module Merb
2
- class FormModel
3
-
4
- end
5
- end