simple_helpers 0.0.4 → 0.0.5
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/CHANGELOG.md +4 -0
- data/README.md +11 -6
- data/lib/simple_helpers/action_controller.rb +2 -40
- data/lib/simple_helpers/support.rb +42 -0
- data/lib/simple_helpers/version.rb +1 -1
- metadata +4 -4
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
Easily create helper methods with I18n and interpolation support to your controllers and views.
|
6
6
|
|
7
|
-
You can configure it to automaticly create some methods (like page_title
|
7
|
+
You can configure it to automaticly create some methods (like page_title) or call the method **simple_helper** manually according to your needs.
|
8
8
|
|
9
9
|
To display page title in your view:
|
10
10
|
|
@@ -22,7 +22,7 @@ require "simple_helpers"
|
|
22
22
|
SimpleHelpers.configure do |config|
|
23
23
|
config.helpers = [:page_title]
|
24
24
|
config.except = [SessionsController]
|
25
|
-
config.only = []
|
25
|
+
# config.only = []
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
@@ -30,14 +30,19 @@ And/or call the method **simple_helper** manually according to your needs:
|
|
30
30
|
|
31
31
|
```ruby
|
32
32
|
# some examples
|
33
|
-
simple_helper :page_subtitle, :
|
33
|
+
simple_helper :page_subtitle, :title => @post.title
|
34
|
+
|
35
|
+
# declare multiple helpers in one statement
|
36
|
+
simple_helper :sponsor, :page_footer, :title => @post.title
|
37
|
+
|
38
|
+
# An instance variable **@<helper-method-name>_options** is created for customizations
|
34
39
|
@sponsor_options.merge!({:company => "Github"})
|
35
40
|
|
36
|
-
|
37
|
-
|
41
|
+
page_footer "Post %{title} by %{author}"
|
42
|
+
@page_footer_options = { :author => @post.author }
|
38
43
|
|
39
44
|
simple_helper :user_alert
|
40
|
-
@user_alert_options = {:username =>
|
45
|
+
@user_alert_options = {:username => current_user.username }
|
41
46
|
```
|
42
47
|
|
43
48
|
If you didn't set the value manually, the gem will get it from your I18n backend.
|
@@ -9,46 +9,8 @@ module SimpleHelpers
|
|
9
9
|
array_of_names.each do |name_symbol|
|
10
10
|
name = name_symbol.underscore
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
if args.empty?
|
15
|
-
send "#{name}_get"
|
16
|
-
else
|
17
|
-
options = args.extract_options!
|
18
|
-
send "#{name}_set", args.first, options
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
define_method("#{name}_options") do
|
23
|
-
instance_variable_set("@#{name}_options", {}) unless instance_variable_get("@#{name}_options")
|
24
|
-
instance_variable_get("@#{name}_options")
|
25
|
-
end
|
26
|
-
|
27
|
-
define_method("#{name}_set") do |*args|
|
28
|
-
instance_variable_set "@#{name}", args.first
|
29
|
-
instance_variable_set "@#{name}_options", args.last
|
30
|
-
end
|
31
|
-
|
32
|
-
define_method("#{name}_get") do |*args|
|
33
|
-
scopes = SimpleHelpers::Support.scopes(self, name)
|
34
|
-
|
35
|
-
value = instance_variable_get("@#{name}")
|
36
|
-
options = send("#{name}_options")
|
37
|
-
|
38
|
-
result = value % options unless value.nil?
|
39
|
-
|
40
|
-
# removing rails reserved word
|
41
|
-
helper_options = options.delete_if{|k,v| k.to_sym == :scope}
|
42
|
-
|
43
|
-
result ||= t(scopes[:first], helper_options)
|
44
|
-
if result.include? "translation missing" and scopes.has_key? :second
|
45
|
-
helper_options.merge! :default => result
|
46
|
-
result = t(scopes[:second], helper_options)
|
47
|
-
end
|
48
|
-
|
49
|
-
result
|
50
|
-
end
|
51
|
-
end
|
12
|
+
# define_methods
|
13
|
+
SimpleHelpers::Support.create_method(self, name)
|
52
14
|
|
53
15
|
# method helper
|
54
16
|
::ActionController::Base.helper_method name.to_s
|
@@ -33,5 +33,47 @@ module SimpleHelpers
|
|
33
33
|
}
|
34
34
|
end
|
35
35
|
|
36
|
+
def self.create_method(controller, name)
|
37
|
+
controller.class_eval do
|
38
|
+
define_method(name) do |*args|
|
39
|
+
if args.empty?
|
40
|
+
send "#{name}_get"
|
41
|
+
else
|
42
|
+
options = args.extract_options!
|
43
|
+
send "#{name}_set", args.first, options
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method("#{name}_options") do
|
48
|
+
instance_variable_set("@#{name}_options", {}) unless instance_variable_get("@#{name}_options")
|
49
|
+
instance_variable_get("@#{name}_options")
|
50
|
+
end
|
51
|
+
|
52
|
+
define_method("#{name}_set") do |*args|
|
53
|
+
instance_variable_set "@#{name}", args.first
|
54
|
+
instance_variable_set "@#{name}_options", args.last
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method("#{name}_get") do |*args|
|
58
|
+
scopes = SimpleHelpers::Support.scopes(controller, name)
|
59
|
+
|
60
|
+
value = instance_variable_get("@#{name}")
|
61
|
+
options = send("#{name}_options")
|
62
|
+
|
63
|
+
result = value % options unless value.nil?
|
64
|
+
|
65
|
+
# removing rails reserved word
|
66
|
+
helper_options = options.delete_if{|k,v| k.to_sym == :scope}
|
67
|
+
|
68
|
+
result ||= t(scopes[:first], helper_options)
|
69
|
+
if result.include? "translation missing" and scopes.has_key? :second
|
70
|
+
helper_options.merge! :default => result
|
71
|
+
result = t(scopes[:second], helper_options)
|
72
|
+
end
|
73
|
+
|
74
|
+
result
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
36
78
|
end
|
37
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -124,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
segments:
|
126
126
|
- 0
|
127
|
-
hash:
|
127
|
+
hash: -4509853982771799667
|
128
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
129
|
none: false
|
130
130
|
requirements:
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
segments:
|
135
135
|
- 0
|
136
|
-
hash:
|
136
|
+
hash: -4509853982771799667
|
137
137
|
requirements: []
|
138
138
|
rubyforge_project:
|
139
139
|
rubygems_version: 1.8.23
|