better_responder 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +119 -0
- data/Rakefile +1 -0
- data/better_responder.gemspec +25 -0
- data/lib/better_responder.rb +5 -0
- data/lib/better_responder/responder.rb +39 -0
- data/lib/better_responder/version.rb +3 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7f5f2ce39582f2986d3bca4b49d15293a1ec1353
|
4
|
+
data.tar.gz: 17d665d01c95e4bb880eaeb9f979b45ae9c50f90
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a8a8c02349467deaf258f16cf78002118a2ec55599fa21a7da7cf5b09b16b7680e33dd83e2e1831ee7dc358cd50ee6624c212d337a2916a737c15ec29cab872b
|
7
|
+
data.tar.gz: f0c15aee9bd2946ee7c0416becd16c37a435d632e8b06cebe1655a03db6e169fca2621c154002bf6009792d65182eec30f7b793d02ee74a9344a4082d4ba8b28
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 sergio1990
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# BetterResponder
|
2
|
+
|
3
|
+
Small tweak for rendering decision.
|
4
|
+
|
5
|
+
## Getting started
|
6
|
+
|
7
|
+
How many times you may see in the controller the code like this:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def create
|
11
|
+
@post = @conversation.posts.build post_params
|
12
|
+
@post.user = current_user
|
13
|
+
if @post.save
|
14
|
+
redirect_to @conversation, notice: I18n.t(...)
|
15
|
+
else
|
16
|
+
render :new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
if @post.update_attributes
|
22
|
+
redirect_to @conversation, notice: I18n.t(...)
|
23
|
+
else
|
24
|
+
render :edit
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy
|
29
|
+
if @post.destroy
|
30
|
+
redirect_to @conversation, notice: I18n.t(...)
|
31
|
+
else
|
32
|
+
redirect_to @conversation, alert: I18n.t(...)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
This code is valid but for me it contains some duplication and it needs to refactor.
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
Add this line to your application's Gemfile:
|
42
|
+
|
43
|
+
gem 'better_responder'
|
44
|
+
|
45
|
+
And then execute:
|
46
|
+
|
47
|
+
$ bundle
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
|
51
|
+
$ gem install better_responder
|
52
|
+
|
53
|
+
Or if you want to stay on the edge:
|
54
|
+
|
55
|
+
gem 'better_responder', github: 'sergio1990/better_responder'
|
56
|
+
|
57
|
+
|
58
|
+
## Usage
|
59
|
+
|
60
|
+
For first you need to initialize Responder object. At this time, I recommend you to do this in Application Controller:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
before_action :create_responder #You must use before_filter for rails < 4
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def create_responder
|
68
|
+
@responder ||= BetterResponder::Responder.new(self)
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
Next, refactor code that was shown earlier:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
def create
|
76
|
+
@post = @conversation.posts.new post_params
|
77
|
+
@post.user = current_user
|
78
|
+
@responder.run @post.save, {redirect_to: @conversation}, {render: :new}
|
79
|
+
end
|
80
|
+
|
81
|
+
def update
|
82
|
+
@responder.run @post.update_attributes(post_params), {redirect_to: @conversation}, {render: :edit}
|
83
|
+
end
|
84
|
+
|
85
|
+
def destroy
|
86
|
+
@responder.run @post.destroy, {redirect_to: @conversation}
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Now we simplify your code by calling single __@responder.run__. IMHO this looks small bit better. However, I dont like to manually calling @responder.run each time and this problem I will solve.
|
91
|
+
|
92
|
+
## Flash messages
|
93
|
+
|
94
|
+
For right using flash messages inside Responder you must declare translations accordingly this pattern:
|
95
|
+
|
96
|
+
```
|
97
|
+
responder.#{controller_name}.#{action_name}.#{type}
|
98
|
+
```
|
99
|
+
|
100
|
+
Type is a finite set of _[success, fail]_.
|
101
|
+
|
102
|
+
For example:
|
103
|
+
|
104
|
+
```yaml
|
105
|
+
ru:
|
106
|
+
responder:
|
107
|
+
posts:
|
108
|
+
destroy:
|
109
|
+
success: Your success message
|
110
|
+
fail: Your fail message
|
111
|
+
```
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
1. Fork it
|
116
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
117
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
118
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
119
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'better_responder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "better_responder"
|
8
|
+
spec.version = BetterResponder::VERSION
|
9
|
+
spec.authors = ["sergio1990"]
|
10
|
+
spec.email = ["sergeg1990@gmail.com"]
|
11
|
+
spec.description = %q{Small tweak for rendering decision}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/sergio1990/better_responder"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'i18n'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module BetterResponder
|
2
|
+
class Responder
|
3
|
+
|
4
|
+
def initialize(context)
|
5
|
+
@context = context
|
6
|
+
end
|
7
|
+
|
8
|
+
def run(condition, success_render, fail_render = nil)
|
9
|
+
provide_response condition ? [success_render, :success] : [fail_render || success_render, :fail]
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def provide_response(params)
|
15
|
+
render_params = params.first
|
16
|
+
render_type = params[1]
|
17
|
+
method = render_params.keys.first
|
18
|
+
@context.send method, render_params[method], :"#{flash_type(render_type)}" => I18n.t(message_builder(render_type))
|
19
|
+
end
|
20
|
+
|
21
|
+
def message_builder(type)
|
22
|
+
"responder.#{controller_name}.#{action_name}.#{type}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def action_name
|
26
|
+
@context.params[:action]
|
27
|
+
end
|
28
|
+
|
29
|
+
def controller_name
|
30
|
+
@context.class.name.underscore.split("_").first
|
31
|
+
end
|
32
|
+
|
33
|
+
def flash_type(render_type)
|
34
|
+
h = {success: :notice, fail: :alert}
|
35
|
+
h[render_type.to_sym]
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_responder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sergio1990
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: i18n
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Small tweak for rendering decision
|
56
|
+
email:
|
57
|
+
- sergeg1990@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- better_responder.gemspec
|
68
|
+
- lib/better_responder.rb
|
69
|
+
- lib/better_responder/responder.rb
|
70
|
+
- lib/better_responder/version.rb
|
71
|
+
homepage: https://github.com/sergio1990/better_responder
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.0.6
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Small tweak for rendering decision
|
95
|
+
test_files: []
|