rails_best_practices 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -44,6 +44,7 @@ NotUseDefaultRouteCheck: { }
44
44
  KeepFindersOnTheirOwnModelCheck: { }
45
45
  LawOfDemeterCheck: { }
46
46
  UseObserverCheck: { }
47
+ IsolateSeedDataCheck: { }
47
48
  </code></pre>
48
49
 
49
50
  *************************************************
@@ -77,7 +78,7 @@ h2. Progress
77
78
  ## [-Use Observer-]
78
79
 
79
80
  * Lesson 4. Migration
80
- ## Isolating Seed Data
81
+ ## [-Isolating Seed Data-]
81
82
  ## Always add DB index
82
83
 
83
84
  * Lesson 5. Controller
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -3,7 +3,7 @@ require 'rails_best_practices/core/error'
3
3
  module RailsBestPractices
4
4
  module Checks
5
5
  class Check
6
- NODE_TYPES = [:call, :defn, :if, :unless, :class]
6
+ NODE_TYPES = [:call, :defn, :if, :unless, :class, :lasgn]
7
7
 
8
8
  def initialize
9
9
  @errors = []
@@ -0,0 +1,46 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ # Check a migration file to make sure not to insert data in migration, move them to seed file.
6
+ #
7
+ # Implementation: check if there are :create, :create!, and :new with :save or :save! exist, the migration file needs isolate seed data.
8
+ class IsolateSeedDataCheck < Check
9
+
10
+ def interesting_nodes
11
+ [:call, :lasgn]
12
+ end
13
+
14
+ def interesting_files
15
+ /db\/migrate\/.*rb/
16
+ end
17
+
18
+ def initialize
19
+ super
20
+ @new_variables = []
21
+ end
22
+
23
+ def evaluate_start(node)
24
+ if [:create, :create!].include? node.message
25
+ add_error("isolate seed data")
26
+ elsif :lasgn == node.node_type
27
+ remember_new_variable(node)
28
+ elsif [:save, :save!].include? node.message
29
+ add_error("isolate seed data") if new_record?(node)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def remember_new_variable(node)
36
+ unless node.grep_nodes({:node_type => :call, :message => :new}).empty?
37
+ @new_variables << node.subject.to_s
38
+ end
39
+ end
40
+
41
+ def new_record?(node)
42
+ @new_variables.include? node.subject.to_ruby
43
+ end
44
+ end
45
+ end
46
+ end
@@ -13,3 +13,4 @@ require 'rails_best_practices/checks/not_use_default_route_check'
13
13
  require 'rails_best_practices/checks/keep_finders_on_their_own_model_check'
14
14
  require 'rails_best_practices/checks/law_of_demeter_check'
15
15
  require 'rails_best_practices/checks/use_observer_check'
16
+ require 'rails_best_practices/checks/isolate_seed_data_check'
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_best_practices}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/rails_best_practices/checks.rb",
30
30
  "lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb",
31
31
  "lib/rails_best_practices/checks/check.rb",
32
+ "lib/rails_best_practices/checks/isolate_seed_data_check.rb",
32
33
  "lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb",
33
34
  "lib/rails_best_practices/checks/law_of_demeter_check.rb",
34
35
  "lib/rails_best_practices/checks/many_to_many_collection_check.rb",
@@ -53,6 +54,7 @@ Gem::Specification.new do |s|
53
54
  "rails_best_practices.gemspec",
54
55
  "rails_best_practices.yml",
55
56
  "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
57
+ "spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
56
58
  "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
57
59
  "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
58
60
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
@@ -81,6 +83,7 @@ Gem::Specification.new do |s|
81
83
  "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
82
84
  "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
83
85
  "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
86
+ "spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
84
87
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
85
88
  "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
86
89
  "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
@@ -13,3 +13,4 @@ NotUseDefaultRouteCheck: { }
13
13
  KeepFindersOnTheirOwnModelCheck: { }
14
14
  LawOfDemeterCheck: { }
15
15
  UseObserverCheck: { }
16
+ IsolateSeedDataCheck: { }
@@ -22,6 +22,24 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
22
22
  errors.should_not be_empty
23
23
  errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
24
24
  end
25
+
26
+ it "should add model virtual attribute with local assignment" do
27
+ content = <<-EOF
28
+ class UsersController < ApplicationController
29
+
30
+ def create
31
+ user = User.new(params[:user])
32
+ user.first_name = params[:full_name].split(' ', 2).first
33
+ user.last_name = params[:full_name].split(' ', 2).last
34
+ user.save
35
+ end
36
+ end
37
+ EOF
38
+ @runner.check('app/controllers/users_controller.rb', content)
39
+ errors = @runner.errors
40
+ errors.should_not be_empty
41
+ errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for user)"
42
+ end
25
43
 
26
44
  it "should not add model virtual attribute with differen param" do
27
45
  content = <<-EOF
@@ -0,0 +1,75 @@
1
+ require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
4
+ before(:each) do
5
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::IsolateSeedDataCheck.new)
6
+ end
7
+
8
+ it "should isolate seed data" do
9
+ content = <<-EOF
10
+ class CreateRoles < ActiveRecord::Migration
11
+ def self.up
12
+ create_table "roles", :force => true do |t|
13
+ t.string :name
14
+ end
15
+
16
+ ["admin", "author", "editor", "account"].each do |name|
17
+ Role.create!(:name => name)
18
+ end
19
+ end
20
+
21
+ def self.down
22
+ drop_table "roles"
23
+ end
24
+ end
25
+ EOF
26
+ @runner.check('db/migrate/20090818130258_create_roles.rb', content)
27
+ errors = @runner.errors
28
+ errors.should_not be_empty
29
+ errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
30
+ end
31
+
32
+ it "should isolate seed data with new and save" do
33
+ content = <<-EOF
34
+ class CreateRoles < ActiveRecord::Migration
35
+ def self.up
36
+ create_table "roles", :force => true do |t|
37
+ t.string :name
38
+ end
39
+
40
+ ["admin", "author", "editor", "account"].each do |name|
41
+ role = Role.new(:name => name)
42
+ role.save!
43
+ end
44
+ end
45
+
46
+ def self.down
47
+ drop_table "roles"
48
+ end
49
+ end
50
+ EOF
51
+ @runner.check('db/migrate/20090818130258_create_roles.rb', content)
52
+ errors = @runner.errors
53
+ errors.should_not be_empty
54
+ errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
55
+ end
56
+
57
+ it "should not isolate seed data without data insert" do
58
+ content = <<-EOF
59
+ class CreateRoles < ActiveRecord::Migration
60
+ def self.up
61
+ create_table "roles", :force => true do |t|
62
+ t.string :name
63
+ end
64
+ end
65
+
66
+ def self.down
67
+ drop_table "roles"
68
+ end
69
+ end
70
+ EOF
71
+ @runner.check('db/migrate/20090818130258_create_roles.rb', content)
72
+ errors = @runner.errors
73
+ errors.should be_empty
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -52,6 +52,7 @@ files:
52
52
  - lib/rails_best_practices/checks.rb
53
53
  - lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
54
54
  - lib/rails_best_practices/checks/check.rb
55
+ - lib/rails_best_practices/checks/isolate_seed_data_check.rb
55
56
  - lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb
56
57
  - lib/rails_best_practices/checks/law_of_demeter_check.rb
57
58
  - lib/rails_best_practices/checks/many_to_many_collection_check.rb
@@ -76,6 +77,7 @@ files:
76
77
  - rails_best_practices.gemspec
77
78
  - rails_best_practices.yml
78
79
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
80
+ - spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb
79
81
  - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
80
82
  - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
81
83
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
@@ -126,6 +128,7 @@ test_files:
126
128
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
127
129
  - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
128
130
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
131
+ - spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb
129
132
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
130
133
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb
131
134
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb