Class: ViewMapper::ModelInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/view_mapper/model_info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name) ⇒ ModelInfo

Returns a new instance of ModelInfo.



9
10
11
12
# File 'lib/view_mapper/model_info.rb', line 9

def initialize(model_name)
  @model = find_model(model_name)
  @name = model.to_s unless model.nil?
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/view_mapper/model_info.rb', line 7

def error
  @error
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/view_mapper/model_info.rb', line 4

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/view_mapper/model_info.rb', line 6

def name
  @name
end

#through_modelObject

has_many :through => XYZ



5
6
7
# File 'lib/view_mapper/model_info.rb', line 5

def through_model
  @through_model
end

Class Method Details

.is_text_field_attrib_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/view_mapper/model_info.rb', line 34

def self.is_text_field_attrib_type?(type)
  [:integer, :float, :decimal, :string].include? type
end

Instance Method Details

#accepts_nested_attributes_for?(child_model) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/view_mapper/model_info.rb', line 99

def accepts_nested_attributes_for?(child_model)
  if !has_method?("#{child_model.name.underscore.pluralize}_attributes=")
    @error = "Model #{model} does not accept nested attributes for model #{child_model.name}."
    false
  else
    true
  end
end

#attachmentsObject



42
43
44
# File 'lib/view_mapper/model_info.rb', line 42

def attachments
  @attachments = find_attachments
end

#attributesObject



30
31
32
# File 'lib/view_mapper/model_info.rb', line 30

def attributes
  @attributes ||= active_record_columns.collect { |col| Rails::Generator::GeneratedAttribute.new col.name, col.type }
end

#belongs_to?(parent_model_name) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/view_mapper/model_info.rb', line 108

def belongs_to?(parent_model_name)
  !association_for(:belongs_to, parent_model_name).nil?
end

#child_modelsObject



54
55
56
# File 'lib/view_mapper/model_info.rb', line 54

def child_models
  @child_models ||= model_info_array_for_association(:has_many)
end

#columnsObject



18
19
20
# File 'lib/view_mapper/model_info.rb', line 18

def columns
  @columns ||= active_record_columns.collect { |col| col.name }
end

#find_through_model(source_model_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/view_mapper/model_info.rb', line 66

def find_through_model(source_model_name)
  reflections_for_association(:has_many, ActiveRecord::Reflection::ThroughReflection).each do |kvpair|
    reflection_model_name = kvpair[0].to_s.singularize
    if reflection_model_name == source_model_name.underscore
      reflection = kvpair[1]
      return ModelInfo.new(reflection.options[:through].to_s.singularize)
    end
  end
  nil
end

#foreign_key_for(parent_model_name) ⇒ Object



129
130
131
# File 'lib/view_mapper/model_info.rb', line 129

def foreign_key_for(parent_model_name)
  model.columns.detect { |col| is_foreign_key_for?(col, parent_model_name) }
end

#has_and_belongs_to_many?(model_name) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/view_mapper/model_info.rb', line 116

def has_and_belongs_to_many?(model_name)
  !association_for(:has_and_belongs_to_many, model_name).nil?
end

#has_attachment?(attachment) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/view_mapper/model_info.rb', line 46

def has_attachment?(attachment)
  attachments.include?(attachment)
end

#has_column?(col_name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/view_mapper/model_info.rb', line 22

def has_column?(col_name)
  model.column_names.include? col_name
end

#has_columns_for_attachment?(attachment) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/view_mapper/model_info.rb', line 50

def has_columns_for_attachment?(attachment)
  !paperclip_columns_for_attachment(attachment).detect { |paperclip_col| !has_column_for_attachment(attachment, paperclip_col) }
end

#has_foreign_key_for?(parent_model_name) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/view_mapper/model_info.rb', line 125

def has_foreign_key_for?(parent_model_name)
  !foreign_key_for(parent_model_name).nil?
end

#has_many?(child_model_name) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/view_mapper/model_info.rb', line 112

def has_many?(child_model_name)
  !association_for(:has_many, child_model_name).nil?
end

#has_many_through?(model_name) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/view_mapper/model_info.rb', line 120

def has_many_through?(model_name)
  reflection = association_for(:has_many, model_name.pluralize)
  reflection && reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
end

#has_many_through_modelsObject



62
63
64
# File 'lib/view_mapper/model_info.rb', line 62

def has_many_through_models
  @has_many_through_models ||= model_info_array_for_association(:has_many, ActiveRecord::Reflection::ThroughReflection)
end

#has_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/view_mapper/model_info.rb', line 26

def has_method?(method_name)
  model.new.methods.include?(method_name) || model.new.methods.include?(method_name.to_sym)
end

#model_info_array_for_association(association_type, type = ActiveRecord::Reflection::AssociationReflection) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/view_mapper/model_info.rb', line 89

def model_info_array_for_association(association_type, type = ActiveRecord::Reflection::AssociationReflection)
  reflections_for_association(association_type, type).collect do |kvpair|
    reflection_model_name = kvpair[0].to_s.singularize
    reflection = kvpair[1]
    model_info_from_reflection(reflection_model_name, reflection)
  end.sort do |a, b|
    a.name <=> b.name
  end
end

#model_info_from_reflection(reflection_model_name, reflection) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/view_mapper/model_info.rb', line 81

def model_info_from_reflection(reflection_model_name, reflection)
  model_info = ModelInfo.new(reflection_model_name)
  if reflection.is_a? ActiveRecord::Reflection::ThroughReflection
    model_info.through_model = ModelInfo.new reflection.options[:through].to_s.singularize
  end
  model_info
end

#parent_modelsObject



58
59
60
# File 'lib/view_mapper/model_info.rb', line 58

def parent_models
  @parent_models ||= model_info_array_for_association(:belongs_to)
end

#reflections_for_association(association_type, type) ⇒ Object



77
78
79
# File 'lib/view_mapper/model_info.rb', line 77

def reflections_for_association(association_type, type)
  model.reflections.select { |key, value| value.macro == association_type && value.is_a?(type) }
end

#text_fieldsObject



38
39
40
# File 'lib/view_mapper/model_info.rb', line 38

def text_fields
  attributes.reject { |attrib| !ModelInfo.is_text_field_attrib_type? attrib.type }.collect { |attrib| attrib.name }
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/view_mapper/model_info.rb', line 14

def valid?
  error.nil?
end