Class: RailsBestPractices::Prepares::ModelPrepare

Inherits:
Core::Check
  • Object
show all
Defined in:
lib/rails_best_practices/prepares/model_prepare.rb

Overview

Remember the model associations.

Constant Summary

Constants inherited from Core::Check

Core::Check::CONTROLLER_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::NODE_TYPES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILE, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Attribute Summary

Attributes inherited from Core::Check

#errors

Instance Method Summary collapse

Methods inherited from Core::Check

#add_error, #method_missing, #node_end, #node_start, #url

Constructor Details

#initializeModelPrepare

Returns a new instance of ModelPrepare.



17
18
19
20
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 17

def initialize
  @models = Core::Models.new
  @model_associations = Core::ModelAssociations.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#association_methodsObject

default rails association methods.



62
63
64
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 62

def association_methods
  [:belongs_to, :has_one, :has_many, :has_and_belongs_to_many]
end

#end_class(class_node) ⇒ Object

assign @model_associations to Prepares.model_associations.



29
30
31
32
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 29

def end_class(class_node)
  Prepares.models = @models
  Prepares.model_associations = @model_associations
end

#interesting_filesObject



13
14
15
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 13

def interesting_files
  MODEL_FILES
end

#interesting_nodesObject



9
10
11
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 9

def interesting_nodes
  [:class, :call]
end

#remember_association(association_node) ⇒ Object

remember associations, with class to association names.



50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 50

def remember_association(association_node)
  association_meta = association_node.message
  association_name = association_node.arguments[1].to_s
  arguments_node = association_node.arguments[2]
  if arguments_node && :hash == arguments_node.node_type
    index = arguments_node.index(s(:lit, :class_name))
    association_class = arguments_node[index + 1].to_s if index
  end
  @model_associations.add_association(@last_klazz, association_name, association_meta, association_class)
end

#start_call(node) ⇒ Object

check call node to remember all assoications.

the remembered association names (@associations) are like

{
  :Project=>{
    "categories" => {:has_and_belongs_to_many => "Category"},
    "project_manager" => {:has_one => "ProjectManager"},
    "portfolio" => {:belongs_to => "Portfolio"},
    "milestones => {:has_many" => "Milestone"}
  }
}


45
46
47
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 45

def start_call(node)
  remember_association(node) if association_methods.include? node.message
end

#start_class(class_node) ⇒ Object

check class node to remember the last class name.



23
24
25
26
# File 'lib/rails_best_practices/prepares/model_prepare.rb', line 23

def start_class(class_node)
  @last_klazz = class_node.class_name.to_s
  @models << @last_klazz
end