youthtree-helpers 0.1.4 → 0.2.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/Rakefile CHANGED
@@ -17,6 +17,7 @@ begin
17
17
  gem.version = YouthTree::Helpers::VERSION
18
18
  gem.add_dependency "activesupport", "~> 3.0.0.rc"
19
19
  gem.add_dependency "actionpack", "~> 3.0.0.rc"
20
+ gem.add_dependency "formtastic", "~> 1.2"
20
21
  gem.add_dependency "youthtree-js"
21
22
  gem.add_dependency "youthtree-settings"
22
23
  end
@@ -2,7 +2,7 @@ require 'active_support/concern'
2
2
 
3
3
  module YouthTree
4
4
  module Helpers
5
- VERSION = "0.1.4".freeze
5
+ VERSION = "0.2.0".freeze
6
6
 
7
7
  extend ActiveSupport::Autoload
8
8
 
@@ -12,6 +12,7 @@ module YouthTree
12
12
  autoload :EmbedHelper
13
13
  autoload :SidebarHelper
14
14
  autoload :UuidTrackerHelper
15
+ autoload :FormBuilder
15
16
 
16
17
  def self.install!
17
18
  ActionView::Base.class_eval do
@@ -22,6 +23,7 @@ module YouthTree
22
23
  include SidebarHelper
23
24
  include UuidTrackerHelper
24
25
  end
26
+ FormBuilder.install!
25
27
  end
26
28
 
27
29
  if defined?(Rails::Railtie)
@@ -29,6 +31,11 @@ module YouthTree
29
31
  initializer "youthtree-helpers.setup-helpers" do
30
32
  YouthTree::Helpers.install!
31
33
  end
34
+
35
+ initializer "youthtree-helpers.setup-i18n" do
36
+ config.i18n.load_path += Dir[Pathname(__FILE__).dirname.join("locales", "**", "*.{yml,rb}")]
37
+ end
38
+
32
39
  end
33
40
  end
34
41
 
@@ -0,0 +1,111 @@
1
+ module YouthTree
2
+ module Helpers
3
+ class FormBuilder < Formtastic::SemanticFormBuilder
4
+
5
+ def self.install!
6
+ Formtastic::SemanticFormHelper.builder = self
7
+ self.include_blank_for_select_by_default = true
8
+ self.i18n_lookups_by_default = true
9
+ self.required_string = proc { ::Formtastic::I18n.t(:required).html_safe }
10
+ self.optional_string = proc { ::Formtastic::I18n.t(:optional).html_safe }
11
+ end
12
+
13
+ def submit(value = "Save changes", options = {})
14
+ @template.content_tag(:button, value, options.reverse_merge(:type => "submit", :id => "#{object_name}_submit"))
15
+ end
16
+
17
+ # Generates a label, using a safe buffer and a few other things along those lines.
18
+ def label(*args)
19
+ super(*args).gsub(/\?\s*\:\<\/label\>/, "?</label>").gsub(/\?\s*\:\s*\<abbr/, "? <abbr")
20
+ end
21
+
22
+ def boolean_input(method, options)
23
+ super.gsub(":</label>", "</label>").gsub(": <abbr", " <abbr")
24
+ end
25
+
26
+ def dob_input(*args)
27
+ options = args.extract_options!
28
+ options.merge!(:start_year => (Time.now.year - 100), :end_year => Time.now.year, :selected => nil)
29
+ args << options
30
+ date_input(*args)
31
+ end
32
+
33
+ def commit_button_with_cancel(*args)
34
+ options = args.extract_options!
35
+ text = options.delete(:label) || args.shift
36
+ cancel_options = options.delete(:cancel)
37
+
38
+ if @object && @object.respond_to?(:new_record?)
39
+ key = @object.new_record? ? :create : :update
40
+
41
+ # Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost)
42
+ # ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost")
43
+ # if there's no i18n, which is pretty crappy. In this circumstance we want to detect this
44
+ # fall back (human_name == name.humanize) and do our own thing name.underscore.humanize ("User Post")
45
+ if @object.class.model_name.respond_to?(:human)
46
+ object_name = @object.class.model_name.human
47
+ else
48
+ object_human_name = @object.class.human_name # default is UserPost => "Userpost", but i18n may do better ("User post")
49
+ crappy_human_name = @object.class.name.humanize # UserPost => "Userpost"
50
+ decent_human_name = @object.class.name.underscore.humanize # UserPost => "User post"
51
+ object_name = (object_human_name == crappy_human_name) ? decent_human_name : object_human_name
52
+ end
53
+ else
54
+ key = :submit
55
+ object_name = @object_name.to_s.send(self.class.label_str_method)
56
+ end
57
+
58
+ text = (self.localized_string(key, text, :action, :model => object_name) ||
59
+ ::Formtastic::I18n.t(key, :model => object_name)) unless text.is_a?(::String)
60
+
61
+ button_html = options.delete(:button_html) || {}
62
+ button_html.merge!(:class => [button_html[:class], key].compact.join(' '))
63
+ element_class = ['commit', options.delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action.
64
+ accesskey = (options.delete(:accesskey) || self.class.default_commit_button_accesskey) unless button_html.has_key?(:accesskey)
65
+ button_html = button_html.merge(:accesskey => accesskey) if accesskey
66
+ inner = Formtastic::Util.html_safe(self.submit(text, button_html))
67
+ # Start custom code
68
+ if cancel_options.present?
69
+ inner << template.content_tag(:span, "or", :class => "or")
70
+ inner << @template.link_to(cancel_options.delete(:text), cancel_options.delete(:url), cancel_options)
71
+ end
72
+ # End custom code
73
+ template.content_tag(:li, inner, :class => element_class)
74
+ end
75
+
76
+ # Returns errors converted to a sentence, adding a full stop.
77
+ def error_sentence(errors, options = {})
78
+ error_class = options[:error_class] || self.class.default_inline_error_class
79
+ error_text = errors.to_sentence.untaint.strip
80
+ error_text << "." unless %w(? ! . :).include?(error_text[-1, 1])
81
+ template.content_tag(:p, Formtastic::Util.html_safe(error_text), :class => error_class)
82
+ end
83
+
84
+ def datetime_picker_input(method, options = {})
85
+ format = (options[:format] || Time::DATE_FORMATS[:default] || '%d %B %Y %I:%M %p')
86
+ string_input(method, datetime_picker_options(format, object.send(method)).merge(options))
87
+ end
88
+
89
+ def date_picker_input(method, options = {})
90
+ format = (options[:format] || Time::DATE_FORMATS[:default] || '%d %B %Y')
91
+ string_input(method, date_picker_options(format, object.send(method)).merge(options))
92
+ end
93
+
94
+ protected
95
+
96
+ def datetime_picker_options(format, value = nil)
97
+ input_options = {:class => 'ui-datetime-picker',:value => value.try(:strftime, format)}
98
+ wrapper_options = {:class => 'datetime-picker'}
99
+ return :wrapper_html => wrapper_options, :input_html => input_options
100
+ end
101
+
102
+ def date_picker_options(format, value = nil)
103
+ input_options = {:class => 'ui-date-picker',:value => value.try(:strftime, format)}
104
+ wrapper_options = {:class => 'date-picker'}
105
+ return :wrapper_html => wrapper_options, :input_html => input_options
106
+ end
107
+
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,9 @@
1
+ en:
2
+ formtastic:
3
+ :yes: 'Yes'
4
+ :no: 'No'
5
+ create: 'Create'
6
+ save: 'Save'
7
+ submit: 'Submit'
8
+ required: ": <abbr class='required' title='This field is required'>(required)</abbr>"
9
+ optional: ":"
@@ -5,35 +5,35 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{youthtree-helpers}
8
- s.version = "0.1.4"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Darcy Laycock"]
12
- s.date = %q{2010-08-25}
12
+ s.date = %q{2010-12-12}
13
13
  s.description = %q{Helpers to make life easier when coding YT-specific apps. see the code..}
14
14
  s.email = %q{sutto@sutto.net}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.md"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.md",
24
- "Rakefile",
25
- "lib/youth_tree/helpers.rb",
26
- "lib/youth_tree/helpers/analytics_helper.rb",
27
- "lib/youth_tree/helpers/assets_helper.rb",
28
- "lib/youth_tree/helpers/embed_helper.rb",
29
- "lib/youth_tree/helpers/general_helper.rb",
30
- "lib/youth_tree/helpers/sidebar_helper.rb",
31
- "lib/youth_tree/helpers/uuid_tracker_helper.rb",
32
- "lib/youthtree-helpers.rb",
33
- "youthtree-helpers.gemspec"
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "lib/youth_tree/helpers.rb",
25
+ "lib/youth_tree/helpers/analytics_helper.rb",
26
+ "lib/youth_tree/helpers/assets_helper.rb",
27
+ "lib/youth_tree/helpers/embed_helper.rb",
28
+ "lib/youth_tree/helpers/form_builder.rb",
29
+ "lib/youth_tree/helpers/general_helper.rb",
30
+ "lib/youth_tree/helpers/sidebar_helper.rb",
31
+ "lib/youth_tree/helpers/uuid_tracker_helper.rb",
32
+ "lib/youth_tree/locales/formtastic.en.yml",
33
+ "lib/youthtree-helpers.rb",
34
+ "youthtree-helpers.gemspec"
34
35
  ]
35
36
  s.homepage = %q{http://github.com/YouthTree/youthtree-helpers}
36
- s.rdoc_options = ["--charset=UTF-8"]
37
37
  s.require_paths = ["lib"]
38
38
  s.rubygems_version = %q{1.3.7}
39
39
  s.summary = %q{A exceedingly simple set of helpers used across Youth Tree web applications.}
@@ -45,17 +45,20 @@ Gem::Specification.new do |s|
45
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
46
  s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0.rc"])
47
47
  s.add_runtime_dependency(%q<actionpack>, ["~> 3.0.0.rc"])
48
+ s.add_runtime_dependency(%q<formtastic>, ["~> 1.2"])
48
49
  s.add_runtime_dependency(%q<youthtree-js>, [">= 0"])
49
50
  s.add_runtime_dependency(%q<youthtree-settings>, [">= 0"])
50
51
  else
51
52
  s.add_dependency(%q<activesupport>, ["~> 3.0.0.rc"])
52
53
  s.add_dependency(%q<actionpack>, ["~> 3.0.0.rc"])
54
+ s.add_dependency(%q<formtastic>, ["~> 1.2"])
53
55
  s.add_dependency(%q<youthtree-js>, [">= 0"])
54
56
  s.add_dependency(%q<youthtree-settings>, [">= 0"])
55
57
  end
56
58
  else
57
59
  s.add_dependency(%q<activesupport>, ["~> 3.0.0.rc"])
58
60
  s.add_dependency(%q<actionpack>, ["~> 3.0.0.rc"])
61
+ s.add_dependency(%q<formtastic>, ["~> 1.2"])
59
62
  s.add_dependency(%q<youthtree-js>, [">= 0"])
60
63
  s.add_dependency(%q<youthtree-settings>, [">= 0"])
61
64
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youthtree-helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darcy Laycock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-25 00:00:00 +08:00
18
+ date: 2010-12-12 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -53,9 +53,24 @@ dependencies:
53
53
  type: :runtime
54
54
  version_requirements: *id002
55
55
  - !ruby/object:Gem::Dependency
56
- name: youthtree-js
56
+ name: formtastic
57
57
  prerelease: false
58
58
  requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ hash: 11
64
+ segments:
65
+ - 1
66
+ - 2
67
+ version: "1.2"
68
+ type: :runtime
69
+ version_requirements: *id003
70
+ - !ruby/object:Gem::Dependency
71
+ name: youthtree-js
72
+ prerelease: false
73
+ requirement: &id004 !ruby/object:Gem::Requirement
59
74
  none: false
60
75
  requirements:
61
76
  - - ">="
@@ -65,11 +80,11 @@ dependencies:
65
80
  - 0
66
81
  version: "0"
67
82
  type: :runtime
68
- version_requirements: *id003
83
+ version_requirements: *id004
69
84
  - !ruby/object:Gem::Dependency
70
85
  name: youthtree-settings
71
86
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
87
+ requirement: &id005 !ruby/object:Gem::Requirement
73
88
  none: false
74
89
  requirements:
75
90
  - - ">="
@@ -79,7 +94,7 @@ dependencies:
79
94
  - 0
80
95
  version: "0"
81
96
  type: :runtime
82
- version_requirements: *id004
97
+ version_requirements: *id005
83
98
  description: Helpers to make life easier when coding YT-specific apps. see the code..
84
99
  email: sutto@sutto.net
85
100
  executables: []
@@ -91,7 +106,6 @@ extra_rdoc_files:
91
106
  - README.md
92
107
  files:
93
108
  - .document
94
- - .gitignore
95
109
  - LICENSE
96
110
  - README.md
97
111
  - Rakefile
@@ -99,9 +113,11 @@ files:
99
113
  - lib/youth_tree/helpers/analytics_helper.rb
100
114
  - lib/youth_tree/helpers/assets_helper.rb
101
115
  - lib/youth_tree/helpers/embed_helper.rb
116
+ - lib/youth_tree/helpers/form_builder.rb
102
117
  - lib/youth_tree/helpers/general_helper.rb
103
118
  - lib/youth_tree/helpers/sidebar_helper.rb
104
119
  - lib/youth_tree/helpers/uuid_tracker_helper.rb
120
+ - lib/youth_tree/locales/formtastic.en.yml
105
121
  - lib/youthtree-helpers.rb
106
122
  - youthtree-helpers.gemspec
107
123
  has_rdoc: true
@@ -109,8 +125,8 @@ homepage: http://github.com/YouthTree/youthtree-helpers
109
125
  licenses: []
110
126
 
111
127
  post_install_message:
112
- rdoc_options:
113
- - --charset=UTF-8
128
+ rdoc_options: []
129
+
114
130
  require_paths:
115
131
  - lib
116
132
  required_ruby_version: !ruby/object:Gem::Requirement
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC