Module: ViewMapper::HasManyExistingView

Includes:
BelongsToParentModels, HasManyChildModels
Defined in:
lib/view_mapper/views/has_many_existing/has_many_existing_view.rb

Instance Attribute Summary

Attributes included from HasManyChildModels

#child_models

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasManyChildModels

#add_child_model_actions, #add_child_model_forms, #add_child_models_manifest, #find_child_models, #is_child_model_action?, #is_model_dependency_action, #is_view_show, #validate_child_model, #validate_child_model_associations, #validate_child_models

Methods included from BelongsToParentModels

#add_model_actions, #field_for, #find_parent_models, #is_model_dependency_action, #model_info_from_param, #parent_models, #validate_parent_model, #validate_parent_models, #virtual_attribute_for, #virtual_attribute_setter_for

Class Method Details

.source_rootObject



7
8
9
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 7

def self.source_root
  File.expand_path(File.dirname(__FILE__) + "/templates")
end

Instance Method Details

#add_through_model_forms(m) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 34

def add_through_model_forms(m)
  m.template(
    "view_show.html.erb",
    File.join('app/views', controller_class_path, controller_file_name, "show.html.erb"),
    { :assigns => { :has_many_through_models => has_many_through_models } }
  )
  m.template(
    "view_form.html.erb",
    File.join('app/views', controller_class_path, controller_file_name, "_form.html.erb")
  )
  has_many_through_models.each do |hmt_model|
    m.template(
      "view_child_form.html.erb",
      File.join('app/views', controller_class_path, controller_file_name, "_#{hmt_model.through_model.name.underscore}.html.erb"),
      { :assigns => { :hmt_model => hmt_model } }

    )
  end
  m.file('nested_attributes.js', 'public/javascripts/nested_attributes.js')
  m
end

#add_through_models_manifest(m) ⇒ Object



28
29
30
31
32
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 28

def add_through_models_manifest(m)
  add_child_model_actions(m) if valid && !view_only?
  add_through_model_forms(m) if valid
  m
end

#find_hmt_modelsObject



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

def find_hmt_models
  if view_param
    view_param.split(',').collect do |param|
      hmt_model_info_from_param(param)
    end
  elsif view_only?
    model.has_many_through_models
  else
    []
  end
end

#has_many_through_modelsObject



61
62
63
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 61

def has_many_through_models
  @hmt_models ||= find_hmt_models
end

#hmt_model_info_from_param(param) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 77

def hmt_model_info_from_param(param)
  if /(.*)\[(.*)\]/.match(param)
    hmt_model = ModelInfo.new($1.singularize)
    select_parent_by hmt_model, $2
  else
    hmt_model = ModelInfo.new(param.singularize)
  end
  hmt_model
end

#manifestObject



20
21
22
23
24
25
26
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 20

def manifest
  m = super.edit do |action|
    action unless is_child_model_action?(action)
  end
  add_through_models_manifest(m)
  m
end

#source_roots_for_viewObject



11
12
13
14
15
16
17
18
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 11

def source_roots_for_view
  [
    HasManyExistingView.source_root,
    HasManyView.source_root,
    File.expand_path(source_root),
    File.join(self.class.lookup('model').path, 'templates')
  ]
end

#validateObject



56
57
58
59
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 56

def validate
  super
  @valid &&= validate_hmt_models
end

#validate_hmt_model(hmt_model) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 102

def validate_hmt_model(hmt_model)
  if !hmt_model.valid?
    logger.error hmt_model.error
    return false
  elsif !hmt_model.has_many_through?(class_name)
    logger.warning "Model #{hmt_model.name} does not contain a has_many through association for #{class_name}."
    return false
  end

  hmt_model.through_model = hmt_model.find_through_model(class_name) if hmt_model.through_model.nil?
  if !hmt_model.through_model.valid?
    logger.error hmt_model.through_model.error
    return false
  elsif !validate_parent_model(hmt_model, hmt_model.through_model.name, hmt_model.through_model)
    return false
  elsif !validate_child_model(hmt_model.through_model, class_name, view_only? ? model : nil)
    return false
  elsif !validate_child_model(hmt_model.through_model, hmt_model.name, nil)
    return false
  end
  true
end

#validate_hmt_modelsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/view_mapper/views/has_many_existing/has_many_existing_view.rb', line 87

def validate_hmt_models
  models = has_many_through_models
  if models.empty?
    if view_only?
      logger.error "No has_many through associations exist in class #{model.name}."
    else
      logger.error "No has_many through association specified."
    end
    return false
  end
  models.reject! { |hmt_model| !validate_hmt_model(hmt_model) }
  @hmt_models = models
  !models.empty?
end