commit_param_routing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tags
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in commit_param_routing.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Senthil V S
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.
@@ -0,0 +1,73 @@
1
+ # CommitParamRouting
2
+
3
+ Many times we might end up using html forms with multiple submit buttons with different behaviours. In those cases, it would be ideal to route
4
+ to different controller actions.
5
+ This gem uses rails [advanced constraints](http://guides.rubyonrails.org/routing.html#advanced-constraints) to get this done.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'commit_param_routing'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install commit_param_routing
20
+
21
+ ## Usage
22
+
23
+ Consider you have a resource and you wanna route to three different actions from the same form with three different submit buttons. Then the route would be
24
+
25
+ ```ruby
26
+ resources :plan do
27
+ post :save, constraints: CommitParamRouting.new(PlansController::SAVE, ProjectsController::REVISE), action: :save
28
+ post :save, constraints: CommitParamRouting.new(PlansController::PROPOSE), action: :propose
29
+ post :save, constraints: CommitParamRouting.new(PlansController::FINALIZE), action: :finalize
30
+ end
31
+ ```
32
+
33
+ And the `plans_controller.rb` would be,
34
+
35
+ ```ruby
36
+ class PlansController < ApplicationController
37
+ SAVE = "Save"
38
+ REVISE = "Revise the plan"
39
+ PROPOSE = "Propose now"
40
+ FINALIZE = "Finalize"
41
+
42
+ def save
43
+ #some code here
44
+ end
45
+
46
+ def propose
47
+ #some code here
48
+ end
49
+
50
+ def finalize
51
+ #some code here
52
+ end
53
+ end
54
+ ```
55
+
56
+ ```haml
57
+ = form_for @plan, url: plan_save_path(@plan), method: :post do |f|
58
+ =# some form elements here
59
+ - if @plan.can_be_revised?
60
+ = f.submit PlansController::REVISE
61
+ - if @plan.can_be_finalized?
62
+ = f.submit PlansController::FINALIZE
63
+ = # other submit buttons
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
73
+
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib/commit_param_routing'
8
+ t.test_files = FileList['test/lib/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'commit_param_routing'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "commit_param_routing"
8
+ spec.version = CommitParamRouting::VERSION
9
+ spec.authors = ["Senthil V S"]
10
+ spec.email = ["vss123@gmail.com"]
11
+ spec.description = %q{Gem that helps rails project to route to different controller actions based on commit param of the form}
12
+ spec.summary = %q{Gem that helps rails project to route to different controller actions based on commit param of the form}
13
+ spec.homepage = "https://github.com/siliconsenthil/commit_param_routing"
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_development_dependency 'activesupport', '>= 3.2'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,11 @@
1
+ class CommitParamRouting
2
+ VERSION = "0.0.1"
3
+
4
+ def initialize(*param_values)
5
+ @param_values = param_values
6
+ end
7
+
8
+ def matches?(request)
9
+ @param_values.any?{|prm| request.params[:commit] == prm.to_s}
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ require "ostruct"
2
+ require_relative '../test_helper'
3
+
4
+ describe CommitParamRouting do
5
+ it "must be defined" do
6
+ CommitParamRouting::VERSION.wont_be_nil
7
+ end
8
+
9
+ describe "matches?" do
10
+ it "should return true when param matches" do
11
+ CommitParamRouting.new(:save).matches?(stub_request("save")).must_equal true
12
+ CommitParamRouting.new(:save).matches?(stub_request("something_else")).must_equal false
13
+ CommitParamRouting.new("save").matches?(stub_request("SaVe")).must_equal false
14
+ end
15
+
16
+ it "should return true when one of the param matches" do
17
+ CommitParamRouting.new(:save, :revise).matches?(stub_request("revise")).must_equal true
18
+ CommitParamRouting.new(:save, :revise).matches?(stub_request("no_revise")).must_equal false
19
+ end
20
+ end
21
+
22
+ private
23
+ def stub_request(commit_param)
24
+ OpenStruct.new(params: {commit: commit_param}.with_indifferent_access)
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'active_support/core_ext'
4
+ require File.expand_path('../../lib/commit_param_routing.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commit_param_routing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Senthil V S
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Gem that helps rails project to route to different controller actions
63
+ based on commit param of the form
64
+ email:
65
+ - vss123@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - commit_param_routing.gemspec
76
+ - lib/commit_param_routing.rb
77
+ - test/lib/commit_param_routing_test.rb
78
+ - test/test_helper.rb
79
+ homepage: https://github.com/siliconsenthil/commit_param_routing
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: 1611573789543045986
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 1611573789543045986
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.25
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Gem that helps rails project to route to different controller actions based
110
+ on commit param of the form
111
+ test_files:
112
+ - test/lib/commit_param_routing_test.rb
113
+ - test/test_helper.rb