rails_zombie 1.0.0

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/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ == Overview
2
+ Auto define controller model instance variable and nested instance variable(s) and assign their
3
+ values from id and nested id
4
+
5
+ == Usage
6
+
7
+ In ApplicationController, include RailsZombie. That's all.
8
+ Example
9
+
10
+
11
+
12
+
13
+
14
+ == Dependencies
15
+
16
+ require 'active_support/inflector'
17
+
18
+ == Install
19
+
20
+ gem install 'rails_zombie'
21
+
@@ -0,0 +1,47 @@
1
+ class Zombie
2
+
3
+ def initialize controller
4
+ @controller = controller
5
+ end
6
+
7
+ def set_resource_instance_variable
8
+ set_inner_instance_variable(@controller.class.name.gsub("Controller","").chop, @controller.params[:id])
9
+ end
10
+
11
+ def set_nested_resources_instance_variables
12
+ nested_models.each do |klass_name, id|
13
+ set_inner_instance_variable(klass_name, id)
14
+ end
15
+ end
16
+
17
+ def set_inner_instance_variable model_klass_name, id
18
+ begin
19
+ klass = model_klass_name.constantize
20
+ end
21
+ instance_variable_name = model_klass_name.underscore
22
+ instance_variable_value = klass.find_by_id(id)
23
+ @controller.instance_variable_set("@#{instance_variable_name}", instance_variable_value)
24
+ rescue
25
+ #model not found
26
+ end
27
+
28
+ private
29
+
30
+ #TODO : get nested model from routes
31
+ def nested_models
32
+ @controller.params.map {|key, value| [key.to_s.gsub('_id','').camelize, value] if key.to_s =~ /_id$/}.compact
33
+ end
34
+
35
+ end
36
+
37
+ module RailsZombie
38
+ def self.included(application)
39
+ application.send(:before_filter, :wake_rails_zombie)
40
+
41
+ def wake_rails_zombie
42
+ zombie = Zombie.new(self)
43
+ zombie.set_resource_instance_variable
44
+ zombie.set_nested_resources_instance_variables
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ module ActionController
2
+ class Base
3
+
4
+ def initialize
5
+ send(@@method)
6
+ end
7
+
8
+ def self.before_filter method
9
+ @@method = method
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), '../../../lib/rails_zombie')
2
+
3
+ class ApplicationController < ActionController::Base
4
+ include RailsZombie
5
+ end
@@ -0,0 +1,2 @@
1
+ class UsersController < ApplicationController
2
+ end
@@ -0,0 +1,8 @@
1
+ class Company
2
+ attr_accessor :id, :name
3
+
4
+ def initialize args={}
5
+ @id = args[:id]
6
+ @name = args[:name]
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class User
2
+ attr_accessor :id, :name, :age
3
+
4
+ def initialize args={}
5
+ @id = args[:id]
6
+ @name = args[:name]
7
+ @age = args[:age]
8
+ end
9
+ end
data/test/help_test.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'app/controllers/action_controller'
3
+ require 'app/controllers/application_controller'
4
+ require 'app/controllers/users_controller'
5
+ require 'app/models/user'
6
+ require 'app/models/company'
7
+
8
+ require 'mocha'
@@ -0,0 +1,54 @@
1
+ require 'help_test'
2
+ require 'active_support/inflector'
3
+
4
+ class TestRailsZombie < Test::Unit::TestCase
5
+
6
+ def setup
7
+ UsersController.any_instance.stubs(:params).returns({:id => 1, :company_id => 1})
8
+ @user = User.new(:id => 1, :name => "Marie", :age => 30)
9
+ @company = Company.new(:id => 1, :name => "Apple")
10
+ User.stubs(:find_by_id).with(1).returns(@user)
11
+ Company.stubs(:find_by_id).with(1).returns(@company)
12
+ end
13
+
14
+ def test_rails_zombie_is_waken!
15
+ UsersController.any_instance.expects(:wake_rails_zombie)
16
+ UsersController.new
17
+ end
18
+
19
+ def test_user_controller_has_user_instance_variable
20
+ controller = UsersController.new
21
+ assert controller.instance_variable_defined?(:@user)
22
+ end
23
+
24
+ def test_user_instance_variable_is_set_with_result_of_find_with_id
25
+ controller = UsersController.new
26
+ assert_equal @user, controller.instance_variable_get(:@user)
27
+ end
28
+
29
+ def test_user_instance_variable_is_set_to_nil_and_no_exeption_is_thrown_when_record_not_found
30
+ User.stubs(:find_by_id).raises
31
+
32
+ assert_nothing_raised do
33
+ controller = UsersController.new
34
+ assert_equal nil, controller.instance_variable_get(:@user)#hmmm , not defined so nil ..
35
+ end
36
+ end
37
+
38
+ def test_should_retrieve_nested_instance_variable
39
+ controller = UsersController.new
40
+
41
+ assert_equal @company, controller.instance_variable_get(:@company)
42
+ end
43
+
44
+ def test_should_not_raise_and_not_create_nested_instance_variable_if_model_does_not_exist
45
+ UsersController.any_instance.stubs(:params).returns({:id => 1, :company_id => 1, :post_id => 1})
46
+
47
+ assert_nothing_raised do
48
+ controller = UsersController.new
49
+ assert_equal @company, controller.instance_variable_get(:@company)
50
+ assert !controller.instance_variable_defined?(:@post)
51
+ end
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_zombie
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Philippe Cantin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-14 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Auto define and assign controller (nested) instances variables from (nested) id
23
+ email:
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - lib/rails_zombie.rb
32
+ - README.rdoc
33
+ - test/app/controllers/action_controller.rb
34
+ - test/app/controllers/application_controller.rb
35
+ - test/app/controllers/users_controller.rb
36
+ - test/app/models/company.rb
37
+ - test/app/models/user.rb
38
+ - test/help_test.rb
39
+ - test/test_rails_zombie.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/anoiaque/rails_zombie
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Auto define and assign controller (nested) instances variables from (nested) id
74
+ test_files:
75
+ - test/app/controllers/action_controller.rb
76
+ - test/app/controllers/application_controller.rb
77
+ - test/app/controllers/users_controller.rb
78
+ - test/app/models/company.rb
79
+ - test/app/models/user.rb
80
+ - test/help_test.rb
81
+ - test/test_rails_zombie.rb