crud_generator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6f1a188657cfb454a7092a607c62c6a778136f3
4
+ data.tar.gz: af92e39bb0328fcb160815136cdfc6cf3f3eee55
5
+ SHA512:
6
+ metadata.gz: 3a4788111b43e68c01c25b730e7ce564c649cfcf75d724f1d7561f3cdb29245ed345eb80c6e19090db83ebb08ce79c6484c76702123efcdbedb8ba82bc983b1d
7
+ data.tar.gz: de9c3d04bac7117cd6c9986daafbba5b8d167ad52f776201a1e19d56394b66c6068bbefdbf2de20ecba48fbb431f27c4f62d4fb12ac277f9ebb7d292cef39a92
@@ -0,0 +1,127 @@
1
+ require 'active_support/inflector'
2
+
3
+ module CRUDGenerator
4
+ def generate_crud_actions(*args)
5
+ actions = [:index, :show, :new, :create, :edit, :update, :destroy]
6
+
7
+ #Allows user input to set the methods one of two ways
8
+ if args.length == 1 && args[0].is_a?(Hash)
9
+ actions = args[0].values.flatten if args[0].keys.first == :only
10
+ actions -= args[0].values.flatten if args[0].keys.first == :except
11
+ elsif !args.empty?
12
+ actions = args
13
+ end
14
+
15
+ actions.each do |action|
16
+ send("define_#{action}")
17
+ end
18
+ end
19
+
20
+ def define_index
21
+ define_method(:index) do
22
+ instance_name = self.class.instance_name
23
+ model_class = self.class.model_class
24
+ instance_variable_set(
25
+ "@#{instance_name.pluralize}",
26
+ model_class.all
27
+ )
28
+ end
29
+ end
30
+
31
+ def define_show
32
+ define_method(:show) do
33
+ instance_name = self.class.instance_name
34
+ model_class = self.class.model_class
35
+ instance_variable_set(
36
+ "@#{instance_name}",
37
+ model_class.find(params[:id])
38
+ )
39
+ end
40
+ end
41
+
42
+ def define_new
43
+ define_method(:new) do
44
+ instance_name = self.class.instance_name
45
+ model_class = self.class.model_class
46
+ instance_variable_set(
47
+ "@#{instance_name}",
48
+ model_class.new
49
+ )
50
+ end
51
+ end
52
+
53
+ def define_edit
54
+ define_method(:edit) do
55
+ instance_name = self.class.instance_name
56
+ model_class = self.class.model_class
57
+ instance_variable_set(
58
+ "@#{instance_name}",
59
+ model_class.find(params[:id])
60
+ )
61
+ end
62
+ end
63
+
64
+ def define_create
65
+ define_method(:create) do
66
+ instance_name = self.class.instance_name
67
+ model_class = self.class.model_class
68
+ instance_variable_set(
69
+ "@#{instance_name}",
70
+ model_class.new(params[instance_name.to_sym])
71
+ )
72
+
73
+ obj = instance_variable_get("@#{instance_name}")
74
+
75
+ if obj.save
76
+ redirect_to obj
77
+ else
78
+ flash.now[:errors] = obj.errors.full_messages
79
+ render :new
80
+ end
81
+ end
82
+ end
83
+
84
+ def define_update
85
+ define_method(:update) do
86
+ instance_name = self.class.instance_name
87
+ model_class = self.class.model_class
88
+ instance_variable_set(
89
+ "@#{instance_name}",
90
+ model_class.find(params[:id])
91
+ )
92
+
93
+ obj = instance_variable_get("@#{instance_name}")
94
+
95
+ if obj.update_attributes(params[instance_name.to_sym])
96
+ redirect_to obj
97
+ else
98
+ flash.now[:errors] = obj.errors.full_messages
99
+ render :edit
100
+ end
101
+ end
102
+ end
103
+
104
+ def define_destroy
105
+ define_method(:destroy) do
106
+ index_url = self.class.index_url
107
+ model_class = self.class.model_class
108
+ obj = model_class.find(params[:id])
109
+
110
+ obj.destroy
111
+ redirect_to index_url
112
+ end
113
+ end
114
+
115
+ def instance_name
116
+ self.to_s.underscore[0..-12].singularize
117
+ end
118
+
119
+ def model_class
120
+ instance_name.camelize.constantize
121
+ end
122
+
123
+ def index_url
124
+ url = root_url
125
+ url += "/#{instance_name.pluralize}"
126
+ end
127
+ end
@@ -0,0 +1,114 @@
1
+ require 'rspec'
2
+ require 'crud_generator'
3
+
4
+ describe CRUDGenerator do
5
+ context "Controller variables" do
6
+ before(:each) do
7
+ class ParentsController
8
+ extend CRUDGenerator
9
+
10
+ def self.root_url
11
+ "localhost:3000"
12
+ end
13
+ end
14
+
15
+ class ChildrenController < ParentsController
16
+ end
17
+ end
18
+
19
+ it "has the right instance name" do
20
+ name1 = ParentsController.instance_name
21
+ name2 = ChildrenController.instance_name
22
+
23
+ name1.should == "parent"
24
+ name2.should == "child"
25
+ end
26
+
27
+ it "has the right model class" do
28
+ Parent = Struct.new(:name)
29
+ Child = Struct.new(:name)
30
+ model1 = ParentsController.model_class
31
+ model2 = ChildrenController.model_class
32
+
33
+ model1.should == (Parent)
34
+ model2.should == (Child)
35
+ end
36
+
37
+ it "has the right index_url" do
38
+ index1 = ParentsController.index_url
39
+ index2 = ChildrenController.index_url
40
+
41
+ index1.should == "localhost:3000/parents"
42
+ index2.should == "localhost:3000/children"
43
+ end
44
+ end
45
+
46
+ context "Controller actions" do
47
+ before(:each) do
48
+ class ParentsController
49
+ extend CRUDGenerator
50
+ include CRUDGenerator
51
+ end
52
+ end
53
+
54
+ it "creates all crud methods automatically" do
55
+ class ChildrenController
56
+ generate_crud_actions
57
+ end
58
+ child1 = ChildrenController.new
59
+
60
+ child1.methods.should include(:index)
61
+ child1.methods.should include(:show)
62
+ child1.methods.should include(:new)
63
+ child1.methods.should include(:create)
64
+ child1.methods.should include(:edit)
65
+ child1.methods.should include(:update)
66
+ child1.methods.should include(:destroy)
67
+ end
68
+
69
+ it "add all and only specified crud methods (when specified)" do
70
+ class MoreChildrenController < ParentsController
71
+ generate_crud_actions :index, :show
72
+ end
73
+ child2 = MoreChildrenController.new
74
+
75
+ child2.methods.should include(:index)
76
+ child2.methods.should include(:show)
77
+ child2.methods.should_not include(:new)
78
+ child2.methods.should_not include(:create)
79
+ child2.methods.should_not include(:edit)
80
+ child2.methods.should_not include(:update)
81
+ child2.methods.should_not include(:destroy)
82
+ end
83
+
84
+ it "only creates included crud methods" do
85
+ class EvenMoreChildrenController < ParentsController
86
+ generate_crud_actions :only => [:index, :show]
87
+ end
88
+ child3 = EvenMoreChildrenController.new
89
+
90
+ child3.methods.should include(:index)
91
+ child3.methods.should include(:show)
92
+ child3.methods.should_not include(:new)
93
+ child3.methods.should_not include(:create)
94
+ child3.methods.should_not include(:edit)
95
+ child3.methods.should_not include(:update)
96
+ child3.methods.should_not include(:destroy)
97
+ end
98
+
99
+ it "creates all crud methods except ones that are excluded" do
100
+ class LastChildrenController < ParentsController
101
+ generate_crud_actions :except => [:index, :show]
102
+ end
103
+ child4 = LastChildrenController.new
104
+
105
+ child4.methods.should_not include(:index)
106
+ child4.methods.should_not include(:show)
107
+ child4.methods.should include(:new)
108
+ child4.methods.should include(:create)
109
+ child4.methods.should include(:edit)
110
+ child4.methods.should include(:update)
111
+ child4.methods.should include(:destroy)
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crud_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Natasha Hull-Richter
8
+ autorequire: crud_generator
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: active_support/inflector
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: CRUD generator generates CRUD dynamically in controllers
42
+ email: natashaalex.hull@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/crud_generator.rb
48
+ - spec/crud_generator_spec.rb
49
+ homepage: http://github.com/NatashaHull/crud_generator
50
+ licenses: []
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: CRUD generator generates CRUD dynamically in controllers
72
+ test_files:
73
+ - spec/crud_generator_spec.rb