simple_helpers 1.0.0 → 2.0.0.beta

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3760bdc12e9255f34b9063c8cd29e114ccf6575d
4
+ data.tar.gz: 78da44fe321d6a08c0e2ad04e1efc06ff790f1ee
5
+ SHA512:
6
+ metadata.gz: 8913a3406650bb585131a0e6b0fa36fac915d1c8f2059284ac47617177e1896a7c6d432f07232656c5b05068b7b35909ed884d5039fe3d8091a548c4528c360c
7
+ data.tar.gz: ae85af3ed3457fec3ca431e88b679a627cd55eb799483b286775d6aca8aea33e2654fcfd5a5853798afc35bc472fc4e80fee486fdd0ddc59d0281e87cebe1479
data/CHANGELOG.md CHANGED
@@ -1,15 +1,3 @@
1
- ## v0.0.4
1
+ ## v2.0.0
2
2
 
3
- * config.blacklist renamed to except and config.whitelist to only
4
-
5
- ## v0.0.3
6
-
7
- * Automatic pluralize I18n scope names is no longer the default behavior
8
-
9
- ## v0.0.2
10
-
11
- * Patch for "#{name}_options" instance_variable_set
12
-
13
- ## v0.0.1
14
-
15
- * initial release
3
+ - I18n support and *easy interpolation*
data/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/vitormil/simple_helpers)
3
3
  [![Build Status](https://secure.travis-ci.org/vitormil/simple_helpers.png)](http://travis-ci.org/vitormil/simple_helpers)
4
4
 
5
- Allow you to easily create helper methods with I18n and interpolation support to be used in your controllers and views.
5
+ Allow you to easily create helper methods with I18n support and **easy interpolation** with instance variables and methods.
6
6
 
7
- You can configure it to automaticly create some methods (like page_title) or call the method **simple_helper** manually according to your needs.
7
+ ## Example
8
8
 
9
9
  To display page title in your view:
10
10
 
@@ -15,134 +15,49 @@ To display page title in your view:
15
15
  </head>
16
16
  ```
17
17
 
18
- ### Usage
19
-
20
- You can configure an automatic behavior to your controllers setting an initializer:
21
-
22
- ```ruby
23
- require "simple_helpers"
24
-
25
- SimpleHelpers.configure do |config|
26
- config.helpers = [:page_title]
27
- config.except = [SessionsController]
28
- # config.only = []
29
- end
30
- ```
31
-
32
- And/or call the method **simple_helper** manually according to your needs:
33
-
34
- ```ruby
35
- # declaring one method
36
- simple_helper :page_subtitle, :title => @post.title
37
-
38
- # declare multiple helpers in one statement
39
- simple_helper :sponsor, :page_footer, :title => @post.title
40
-
41
- # An instance variable **@<helper-method-name>_options** is created for customizations
42
- @sponsor_options.merge!({:company => "Github"})
43
-
44
- page_footer "Post %{title} by %{author}"
45
- @page_footer_options = { :author => @post.author }
46
-
47
- simple_helper :user_alert
48
- @user_alert_options = {:username => current_user.username }
49
-
50
- # filling in the value manually
51
- page_title @post.title
52
- ```
53
-
54
- If you didn't set the value manually, the gem will get it from your I18n backend.
18
+ Create a file `./config/locale/simple_helpers.yml` (could be another name) to store the values, following the convention: `<helper>.<controller>.<action>`
55
19
 
56
20
  ```ruby
57
21
  en:
58
22
  page_title:
59
- simple_helper_default:
60
- "Default page title"
23
+ posts:
24
+ new: "New Post"
25
+ show: "{{@post.title}}"
26
+ edit: "Editing {{@post.title}}"
27
+ search: "Search results for {{@query}}..."
61
28
  users:
62
- new: "Sign up"
63
- show: "%{name}'s Page"
64
- page_subtitle:
65
- simple_helper_default:
66
- "Default page subtitle"
67
- user_alert:
68
- users:
69
- index: "This alert goes to %{username}"
70
- sponsor:
71
- simple_helper_default:
72
- "This article %{title} is sponsored by %{company}."
73
- ```
74
-
75
- ### Interpolation
76
-
77
- In many cases you want to abstract your translations so that variables can be interpolated into the translation, just like Rails does.
78
-
79
- ```ruby
80
- page_title :name => "John Doe"
81
- or
82
- @page_title_options.merge!({:name => "John Doe"})
83
- or
84
- simple_helper :page_title, :name => "John Doe"
29
+ show: "{{@user.name}}'s profile"
30
+ new: "{{get_title}}"
31
+ edit: "Edit user"
32
+ destroy: "Delete user"
85
33
  ```
86
34
 
87
- ### Aliases
88
-
89
- There are some action aliases:
90
-
91
- ```ruby
92
- "create" => "new"
93
- "update" => "edit"
94
- "remove" => "destroy"
95
- ```
35
+ ### Easy Interpolation
96
36
 
97
- I18n backend chain:
37
+ Simple helpers will look for instance variables and methods in the controller, and get the value to interpolate with the i18n values. Awesome!!! :)
98
38
 
99
- ```ruby
100
- en:
101
- <helper method name>:
102
- simple_helper_default:
103
- "My default data"
104
- <controller>:
105
- <action>: "My custom data for this action"
106
- ```
39
+ ### Configuration
107
40
 
108
- Adding custom aliases:
41
+ Initializer `./config/initializers/simple_helpers.rb`
109
42
 
110
43
  ```ruby
111
- class PostsController < ApplicationController
44
+ require "simple_helpers"
112
45
 
113
- SIMPLE_HELPER_ALIASES = {
114
- "the_custom_action" => "index"
46
+ SimpleHelpers.configure do |config|
47
+ config.helpers = {
48
+ page_title: { prefix: "MyBlog", separator: " - " }
115
49
  }
116
-
117
- def index
118
- ...
119
- end
120
-
121
- def the_custom_action
122
- ...
123
- end
124
-
125
50
  end
126
51
  ```
127
52
 
128
- This means that the I18n scope for the action "the_custom_action" will be the same as "index".
129
-
130
- You can also specify a custom location at the "scope" key at the options hash, exactly like Rails does.
131
-
132
- ```ruby
133
- simple_helper :page_title, :scope => "my.awesome.chain", :name => "John"
134
- or
135
- simple_helper :page_title
136
- @page_title_options = {:scope => "my.awesome.chain", :name => "John"}
137
- ```
53
+ ### Aliases
138
54
 
139
- Make sure you have this scope defined in a locale file:
55
+ There are some action aliases:
140
56
 
141
57
  ```ruby
142
- en:
143
- my:
144
- awesome:
145
- chain: "What's up %{name}?"
58
+ "create" => "new"
59
+ "update" => "edit"
60
+ "remove" => "destroy"
146
61
  ```
147
62
 
148
63
  ## Getting started
@@ -169,20 +84,10 @@ After you install and add it to your Gemfile, you need to run the generator to c
169
84
  rails generate simple_helpers
170
85
  ```
171
86
 
172
- Take a look at the generated file:
173
-
174
- https://github.com/vitormil/simple_helpers/blob/master/templates/initializer.rb
175
-
176
87
  ## Maintainer
177
88
 
178
89
  * Vitor Oliveira (<http://github.com/vitormil>)
179
90
 
180
- ## Special thanks
181
-
182
- I have been learning a lot with Nando Vieira and this gem was inspired by the "page_title" feature from his gem @swiss_knife.
183
-
184
- Thanks @fnando! (<http://github.com/fnando>)
185
-
186
91
  ## Contributing
187
92
 
188
93
  1. Fork it
@@ -1,22 +1,14 @@
1
1
  module SimpleHelpers
2
2
  module ActionController
3
- def simple_helper(*name_or_array_of_names)
4
- main_options = name_or_array_of_names.extract_options!
5
- array_of_names = SimpleHelpers::Support.certified_array!(name_or_array_of_names)
3
+ def simple_helper(helpers_array)
4
+ helpers_array.each do |name|
5
+ name = name.to_s.underscore
6
6
 
7
- SimpleHelpers::Support.log "Defining methods #{array_of_names.inspect} to #{self.class.inspect}"
8
-
9
- array_of_names.each do |name_symbol|
10
- name = name_symbol.underscore
11
-
12
- # define_methods
7
+ # method
13
8
  SimpleHelpers::Support.create_method(self, name)
14
9
 
15
- # method helper
16
- ::ActionController::Base.helper_method name.to_s
17
-
18
- # initialize
19
- instance_variable_set "@#{name}_options", Hash[main_options] unless main_options.empty?
10
+ # helper
11
+ ::ActionController::Base.helper_method name
20
12
  end
21
13
  end
22
14
  end
@@ -1,36 +1,11 @@
1
1
  module SimpleHelpers
2
2
  class Config
3
-
4
- VALID_OPTIONS = [:log, :pluralize]
5
-
6
- def self.only
7
- @only ||= []
8
- end
9
-
10
- def self.only=(*allowed)
11
- @only = SimpleHelpers::Support.certified_array!(allowed)
12
- end
13
-
14
- def self.except
15
- @except ||= []
16
- end
17
-
18
- def self.except=(*not_allowed)
19
- @except = SimpleHelpers::Support.certified_array!(not_allowed)
20
- end
21
-
22
3
  def self.helpers
23
- @helpers ||= []
4
+ @helpers ||= {}
24
5
  end
25
6
 
26
- def self.helpers=(*helper_methods)
27
- @helpers = SimpleHelpers::Support.certified_array!(helper_methods)
28
- end
29
-
30
- def self.allowed_controller?(controller)
31
- ( @only.empty? and @except.empty? ) or
32
- ( not @only.empty? and @only.include?(controller) ) or
33
- ( not @except.empty? and not @except.include?(controller) )
7
+ def self.helpers=(helper_methods)
8
+ @helpers = helper_methods
34
9
  end
35
10
 
36
11
  def self.options
@@ -45,6 +20,5 @@ module SimpleHelpers
45
20
  def self.has_option?(option)
46
21
  @options.include?(option.to_sym)
47
22
  end
48
-
49
23
  end
50
24
  end
@@ -14,11 +14,7 @@ module SimpleHelpers
14
14
  initialize_method = instance_method(:initialize)
15
15
 
16
16
  define_method :initialize do |*args|
17
- if SimpleHelpers::Config.allowed_controller?(self.class.name)
18
- simple_helper(SimpleHelpers::Config.helpers)
19
- else
20
- SimpleHelpers::Support.log "Controller #{self.class.inspect} not allowed."
21
- end
17
+ simple_helper(SimpleHelpers::Config.helpers.keys)
22
18
 
23
19
  initialize_method.bind(self).call(*args)
24
20
  end
@@ -1,19 +1,11 @@
1
1
  module SimpleHelpers
2
2
  class Support
3
-
4
- def self.log(message)
5
- Rails.logger.debug "SimpleHelpers => #{message}" if SimpleHelpers::Config.has_option? :log
6
- end
7
-
8
3
  def self.certified_array!(splat_arg)
9
4
  array = splat_arg.first.is_a?(Array) ? splat_arg.first : Array(splat_arg)
10
5
  array.collect{|item| item.to_s}
11
6
  end
12
7
 
13
8
  def self.scopes(controller, method_name)
14
- options = controller.send "#{method_name}_options"
15
- return {:first => options[:scope]} if options.has_key? :scope
16
-
17
9
  controller_name = controller.class.name.underscore
18
10
  controller_name.gsub!(/\//, "_")
19
11
  controller_name.gsub!(/_controller$/, "")
@@ -25,7 +17,6 @@ module SimpleHelpers
25
17
  action_name = simple_helper_aliases.fetch(action_name, action_name) if simple_helper_aliases.is_a? Hash
26
18
  end
27
19
  group = method_name
28
- group = group.pluralize if options.has_key? :pluralize
29
20
 
30
21
  {
31
22
  :first => "#{group}.#{controller_name}.#{action_name.to_s}",
@@ -33,47 +24,83 @@ module SimpleHelpers
33
24
  }
34
25
  end
35
26
 
27
+ def self.template(controller, context)
28
+ context.scan(/{{.*?}}/i).each do |key|
29
+ parts = key.delete("{}").split(".")
30
+
31
+ begin
32
+ first_argument = parts.shift
33
+ if first_argument.start_with? "@"
34
+ instance_variable = controller.instance_variable_get(first_argument)
35
+
36
+ value = if parts.empty?
37
+ instance_variable
38
+ else
39
+ thing = parts.shift.to_sym
40
+ instance_variable[thing] if instance_variable.respond_to?(thing)
41
+ end
42
+ else
43
+ value = controller.send first_argument
44
+ end
45
+ value = "" unless value
46
+ rescue
47
+ value = ""
48
+ ensure
49
+ context.sub!(key, value)
50
+ end
51
+ end
52
+ context
53
+ end
54
+
55
+ def self.translate(scope, options)
56
+ begin
57
+ _options = options || {}
58
+ I18n.translate!(scope, _options.merge({raise: true}))
59
+ rescue I18n::MissingTranslationData
60
+ ""
61
+ end
62
+ end
63
+
64
+ def self.get_content(scopes, options)
65
+ result = SimpleHelpers::Support.translate(scopes[:first], options)
66
+ if result.empty?
67
+ result = SimpleHelpers::Support.translate(scopes[:second], options)
68
+ end
69
+ result
70
+ end
71
+
36
72
  def self.create_method(controller, name)
37
73
  controller.class_eval do
38
74
  define_method(name) do |*args|
39
75
  if args.empty?
40
76
  send "#{name}_get"
41
77
  else
42
- options = args.extract_options!
43
- send "#{name}_set", args.first, options
78
+ send "#{name}_set", args.first
44
79
  end
45
80
  end
46
81
 
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
82
  define_method("#{name}_set") do |*args|
53
83
  instance_variable_set "@#{name}", args.first
54
- instance_variable_set "@#{name}_options", args.last
55
84
  end
56
85
 
57
86
  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?
87
+ scopes = SimpleHelpers::Support.scopes(controller, name)
88
+ options_hash = instance_variable_get("@#{name}_options")
89
+ result = SimpleHelpers::Support.get_content(scopes, options_hash)
90
+ helper_options = SimpleHelpers::Config.helpers[name.to_sym]
64
91
 
65
- # removing rails reserved word
66
- helper_options = options.delete_if{|k,v| k.to_sym == :scope}
92
+ prefix = helper_options.fetch(:prefix, "")
93
+ content = SimpleHelpers::Support.template(controller, result)
94
+ separator = if prefix.empty? || content.empty?
95
+ ""
96
+ else
97
+ helper_options.fetch(:separator, " - ")
98
+ end
67
99
 
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
100
+ "#{prefix}#{separator}#{content}"
75
101
  end
76
102
  end
77
103
  end
104
+
78
105
  end
79
106
  end
@@ -1,8 +1,8 @@
1
1
  module SimpleHelpers
2
2
  module Version
3
- MAJOR = 1
3
+ MAJOR = 2
4
4
  MINOR = 0
5
5
  PATCH = 0
6
- STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}.beta"
7
7
  end
8
8
  end
@@ -11,8 +11,6 @@ module SimpleHelpers
11
11
  end
12
12
 
13
13
  SimpleHelpers.configure do |config|
14
- config.helpers = []
15
- config.except = []
16
- config.only = []
17
- config.options = [:log]
14
+ config.helpers = {}
15
+ config.options = []
18
16
  end
@@ -1,23 +1,8 @@
1
- # Use this file to setup SimpleHelpers.
2
1
  require "simple_helpers"
3
2
 
4
3
  SimpleHelpers.configure do |config|
5
- # Helper methods that will be automatically
6
- # created in the controllers
7
- # config.helpers = [:page_title]
8
-
9
- # config.helpers will NOT be created automatically in these controllers
10
- # *** keep it empty if you would allow all controlers
11
- # example: [SessionsController]
12
- # config.except = []
13
-
14
- # config.helpers will be created automatically in these controllers
15
- # *** keep it empty if you would allow all controlers
16
- # example: [PostsController]
17
- # config.only = []
18
-
19
- # Options:
20
- # :pluralize => I18n pluralized helper names
21
- # [ :log, :pluralize ]
22
- # config.options = [ :log ]
4
+ # Helper methods that will be automatically created in the controllers
5
+ # config.helpers = {
6
+ # page_title: { prefix: "MySite", separator: " - " }
7
+ # }
23
8
  end
metadata CHANGED
@@ -1,78 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 2.0.0.beta
6
5
  platform: ruby
7
6
  authors:
8
7
  - Vitor Oliveira
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-10 00:00:00.000000000 Z
11
+ date: 2013-05-01 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec-rails
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: Customizable helper methods with I18n support.
@@ -111,33 +102,26 @@ files:
111
102
  - templates/initializer.rb
112
103
  homepage: http://rubygems.org/gems/simple_helpers
113
104
  licenses: []
105
+ metadata: {}
114
106
  post_install_message:
115
107
  rdoc_options: []
116
108
  require_paths:
117
109
  - lib
118
110
  required_ruby_version: !ruby/object:Gem::Requirement
119
- none: false
120
111
  requirements:
121
- - - ! '>='
112
+ - - '>='
122
113
  - !ruby/object:Gem::Version
123
114
  version: '0'
124
- segments:
125
- - 0
126
- hash: -2281090930503937938
127
115
  required_rubygems_version: !ruby/object:Gem::Requirement
128
- none: false
129
116
  requirements:
130
- - - ! '>='
117
+ - - '>'
131
118
  - !ruby/object:Gem::Version
132
- version: '0'
133
- segments:
134
- - 0
135
- hash: -2281090930503937938
119
+ version: 1.3.1
136
120
  requirements: []
137
121
  rubyforge_project:
138
- rubygems_version: 1.8.23
122
+ rubygems_version: 2.0.3
139
123
  signing_key:
140
- specification_version: 3
124
+ specification_version: 4
141
125
  summary: Customizable helper methods with I18n support.
142
126
  test_files:
143
127
  - spec/controllers/application_controller_spec.rb