i_helpers 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,158 @@
1
+ require 'active_support'
2
+ require 'action_controller'
3
+
4
+ BASE_DIR = File.dirname(__FILE__)
5
+ require BASE_DIR + '/i_helpers/active_record/validations'
6
+
7
+ module IHelpers
8
+
9
+ mattr_accessor :title_format, :title_print_in_place, :small_icons_location
10
+
11
+ self.small_icons_location = "icons/small/"
12
+ self.title_format = "$title - $sitename"
13
+ self.title_print_in_place = true
14
+
15
+ def focus_on id
16
+ javascript_tag "$('#{id}').focus()";
17
+ end
18
+
19
+ def n2br(str)
20
+ str.gsub("\n", "<br />")
21
+ end
22
+
23
+ def javascript(*files)
24
+ options = files.extract_options!
25
+ options.reverse_merge! :placement => :head
26
+ content_for(options[:placement]) { javascript_include_tag(*files) }
27
+ end
28
+
29
+ def stylesheet(*files)
30
+ options = files.extract_options!
31
+ options.reverse_merge! :placement => :head
32
+ content_for(options[:placement]) { stylesheet_link_tag(*files) }
33
+ end
34
+
35
+ # New version inspired by
36
+ # title_helper: http://github.com/DefV/title_helper/tree/master
37
+ def title(*args)
38
+ options = args.extract_options! || {}
39
+
40
+ options.delete([:page, :bar]) # for (at least a bit of) backwards compatibility
41
+
42
+ if args.length > 0
43
+ in_place_title = args[0]
44
+ @title = in_place_title.gsub(/<\/?[^>]*>/, "")
45
+ if IHelpers.title_print_in_place
46
+ options[:class] = [options[:class], 'error'].compact.join(' ') if options[:error]
47
+ return content_tag(:h1, [options[:header], in_place_title, options[:trailer]].compact.join(' '), options.except(:error, :header, :trailer))
48
+ end
49
+ else
50
+ sitename = options[:site_name]
51
+ if @title && !sitename.blank?
52
+ return IHelpers.title_format.gsub('$title', h(@title)).gsub('$sitename', h(sitename))
53
+ elsif @title && sitename.blank?
54
+ return h(@title)
55
+ else
56
+ return "#{sitename}"
57
+ end
58
+ end
59
+ end
60
+
61
+ def small_icon(icon, options = {})
62
+ options.reverse_merge! :class => "icon"
63
+ prefix = IHelpers.small_icons_location
64
+ if options[:grayed_out]
65
+ options.delete :grayed_out
66
+ prefix += "grayed_out/"
67
+ end
68
+ image_tag("#{prefix}#{icon}.png" , options)
69
+ end
70
+
71
+ def link_with_icon_to(options = {})
72
+ r = link_with_icon(options)
73
+ options[:link_options].reverse_merge!(:class => "with_icon")
74
+ r = link_to(r, options[:url], options[:link_options]) if !options[:disabled]
75
+ r
76
+ end
77
+
78
+ def link_with_icon_to_remote(options = {})
79
+ r = link_with_icon(options)
80
+ options.reverse_merge!(:link_html_options => {})
81
+ options.reverse_merge!(:method => :post)
82
+ options[:link_options].reverse_merge!({:method => options[:method]})
83
+ options[:link_html_options].reverse_merge!(:class => "with_icon")
84
+ r = link_to_remote(r, {:url => options[:url]}.merge(options[:link_options]),
85
+ options[:link_html_options]) if !options[:disabled]
86
+ r
87
+ end
88
+
89
+ def flash_tag
90
+ r = ""
91
+ notice_style = "display: none;" if !flash[:notice]
92
+ error_style = "display: none;" if !flash[:error]
93
+ r << content_tag(:div, flash[:notice], {:id => "flash_notice", :style => notice_style})
94
+ r << content_tag(:div, flash[:error], {:id => "flash_error", :style => error_style})
95
+ end
96
+
97
+ def update_flash
98
+ r = ""
99
+ update_page do |page|
100
+ if flash[:notice]
101
+ r << (page.replace_html :flash_notice, flash[:notice])
102
+ r << (page.show :flash_notice)
103
+ flash.discard :notice
104
+ end
105
+ if flash[:error]
106
+ r << (page.replace_html :flash_error, flash[:error])
107
+ r << (page.show :flash_error)
108
+ flash.discard :error
109
+ end
110
+ end
111
+ r
112
+ end
113
+
114
+ def discard_flash
115
+ r = ""
116
+ update_page do |page|
117
+ r << (page.replace_html :flash_notice, nil)
118
+ r << (page.hide :flash_notice)
119
+ flash.discard :notice if flash[:notice]
120
+ r << (page.replace_html :flash_error, nil)
121
+ r << (page.hide :flash_error)
122
+ flash.discard :error if flash[:error]
123
+ end
124
+ r
125
+ end
126
+
127
+
128
+ protected
129
+ def link_with_icon(options = {})
130
+ options.symbolize_keys!
131
+ options.reverse_merge!(:show_text => true, :show_icon => true, :disabled => false,
132
+ :icon_options => {}, :link_options => {}, :text_options => {})
133
+ options[:icon_options].reverse_merge!(:alt => options[:text])
134
+ if options[:disabled]
135
+ options[:icon_options].reverse_merge!(:grayed_out => true)
136
+ options[:text_options].reverse_merge!(:class => "icon_text_grayed_out")
137
+ else
138
+ options[:text_options].reverse_merge!(:class => "icon_text")
139
+ end
140
+ return "" unless options.has_key?(:icon) || options.has_key?(:text)
141
+
142
+ r = ""
143
+ r << small_icon(options[:icon], options[:icon_options]) if options[:show_icon]
144
+ r << content_tag(:span, options[:text], options[:text_options]) if options[:show_text] && options[:text]
145
+ r
146
+ end
147
+ end
148
+
149
+ require 'i_helpers/active_record/validations'
150
+
151
+ ActionController::Base.helper IHelpers
152
+
153
+ class ActiveRecord::Base
154
+ include IHelpers::ActiveRecord::Validations
155
+ end
156
+
157
+ # Load locales from +locale+ directory into Rails
158
+ I18n.load_path += Dir[ File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}') ]
@@ -0,0 +1,54 @@
1
+ module IHelpers
2
+ module ActiveRecord
3
+ module Validations
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ # Taken from: http://marklunds.com/articles/one/312
12
+ def validates_as_email(*attr_names)
13
+ configuration = {
14
+ :message => :email,
15
+ :with => Format::EMAIL, # Requires simply_useful gem
16
+ }
17
+ configuration.update(attr_names.extract_options!)
18
+
19
+ validates_format_of attr_names, configuration
20
+ end
21
+
22
+ # Taken from: http://marklunds.com/articles/one/312
23
+ def validates_as_phone(*attr_names)
24
+ configuration = {
25
+ :message => :phone,
26
+ :on => :save
27
+ }
28
+ configuration.update(attr_names.extract_options!)
29
+
30
+ validates_each(attr_names, configuration) do |record, attr_name, value|
31
+ n_digits = value.scan(/[0-9]/).size
32
+ valid_chars = (value =~ /^[+\/\-() 0-9]+$/)
33
+ if !(n_digits > 5 && valid_chars)
34
+ record.errors.add(attr_name, configuration[:message])
35
+ end
36
+ end
37
+ end
38
+
39
+ def validates_as_immutable(*attr_names)
40
+ options = attr_names.extract_options!
41
+ options.reverse_merge!({:message => :immutable, :allow_change_from_nil => false, :on => :save})
42
+
43
+ validates_each(attr_names, options) do |record, attr_name, value|
44
+ if !record.new_record? && record.send("#{attr_name}_changed?") &&
45
+ !(options[:allow_change_from_nil] && record.send("#{attr_name}_was").nil?)
46
+ record.errors.add(attr_name, options[:message])
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Maciej Bilas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-11 00:00:00 +02:00
18
+ date: 2010-10-29 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,8 +28,9 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README
30
30
  files:
31
- - spec/debug.log
32
31
  - README
32
+ - lib/i_helpers.rb
33
+ - lib/i_helpers/active_record/validations.rb
33
34
  - spec/i_helpers_spec.rb
34
35
  - spec/spec_helper.rb
35
36
  has_rdoc: true
@@ -1 +0,0 @@
1
- # Logfile created on Sat Sep 11 16:58:44 +0200 2010 by logger.rb/22285