auto-assignment 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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in auto-assignment.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ end
8
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 paranoiase Kang
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,29 @@
1
+ # Auto::Assignment
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'auto-assignment'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install auto-assignment
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/auto_assignment', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["paranoiase Kang"]
6
+ gem.email = ["paranoiase@gmail.com"]
7
+ gem.description = "Auto assign model resource and collection from controller"
8
+ gem.summary = %q{Auto assignment of ruby on rails}
9
+ gem.homepage = "http://www.twitter.com/paranoiase"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(spec)/})
14
+ gem.name = "auto-assignment"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AutoAssignment::VERSION
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec', '~> 2.7'
20
+ gem.add_development_dependency 'rspec-rails', '~> 2.7'
21
+ gem.add_development_dependency 'guard-rspec'
22
+ gem.add_development_dependency 'activesupport', '~> 3.2'
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'action_controller'
2
+
3
+ module AutoAssignment
4
+ VERSION = "0.0.1"
5
+
6
+ module ClassMethods
7
+ def auto_assignment(resource_name=nil, options={})
8
+ resource_name = controller_name.singularize unless resource_name
9
+ resource_name = resource_name.to_s
10
+
11
+ before_filter do |controller|
12
+ auto_assignment(resource_name, options)
13
+ end
14
+ end
15
+ end
16
+
17
+ def auto_assignment(resource_name, options)
18
+ collection_name = resource_name.tableize
19
+ model_name = collection_name.classify.constantize
20
+
21
+ collection = model_name.scoped
22
+ resource = if params[:id].blank?
23
+ collection.new
24
+ else
25
+ collection.find(params[:id])
26
+ end
27
+
28
+ if params[:post]
29
+ resource.attributes = params[:post]
30
+ end
31
+
32
+ instance_variable_set('@posts', collection)
33
+ instance_variable_set('@post', resource)
34
+ end
35
+
36
+ def self.included(base)
37
+ base.extend ClassMethods
38
+ end
39
+ end
40
+
41
+ ActiveSupport.on_load(:action_controller) do
42
+ include AutoAssignment
43
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe AutoAssignment do
4
+ let!(:new_post) { Post.new(title: "new post") }
5
+ let!(:exist_post) { Post.new(title: "exist post") }
6
+
7
+ before do
8
+ Post.stub(:new) { new_post }
9
+ Post.stub(:find) { exist_post }
10
+ end
11
+
12
+ it "should assign new resource without id" do
13
+ controller = PostsController.new
14
+ controller.auto_assignment('post', {})
15
+ controller.post.should == new_post
16
+ end
17
+
18
+ it "should assign exist resource with id" do
19
+ controller = PostsController.new
20
+ controller.params = {id: 1}
21
+ controller.auto_assignment('post', {})
22
+ controller.post.should == exist_post
23
+ end
24
+
25
+ it "should assign resource's attributes" do
26
+ controller = PostsController.new
27
+ controller.params = {post: {description: "Hello World"}}
28
+ controller.auto_assignment('post', {})
29
+ controller.post.description.should == "Hello World"
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ class Post
2
+ attr_accessor :title, :description
3
+
4
+ def self.scoped
5
+ self
6
+ end
7
+
8
+ def initialize(params)
9
+ attributes = params
10
+ end
11
+
12
+ def attributes=(params)
13
+ @title = params[:title]
14
+ @description = params[:description]
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ class PostsController < ActionController::Base
2
+ auto_assignment
3
+
4
+ attr_accessor :post, :posts
5
+ attr_accessor :params
6
+
7
+ def initialize
8
+ @params = {}
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ require './spec/mocks/post'
2
+ require './spec/mocks/posts_controller'
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto-assignment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - paranoiase Kang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.7'
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: '2.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.7'
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: '2.7'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activesupport
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '3.2'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '3.2'
94
+ description: Auto assign model resource and collection from controller
95
+ email:
96
+ - paranoiase@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - Guardfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - auto-assignment.gemspec
109
+ - lib/auto_assignment.rb
110
+ - spec/auto_assignment_spec.rb
111
+ - spec/mocks/post.rb
112
+ - spec/mocks/posts_controller.rb
113
+ - spec/spec_helper.rb
114
+ homepage: http://www.twitter.com/paranoiase
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.23
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Auto assignment of ruby on rails
138
+ test_files:
139
+ - spec/auto_assignment_spec.rb
140
+ - spec/mocks/post.rb
141
+ - spec/mocks/posts_controller.rb
142
+ - spec/spec_helper.rb
143
+ has_rdoc: