Module: ViewMapper::PaperclipView

Defined in:
lib/view_mapper/views/paperclip/paperclip_view.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



4
5
6
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 4

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

Instance Method Details

#add_model_actions(m) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 22

def add_model_actions(m)
  m.directory(File.join('test/fixtures', class_path))
  m.template   'model.rb',     File.join('app/models', class_path, "#{file_name}.rb")
  m.template   'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
  unless options[:skip_fixture]
    m.template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")
  end
  unless options[:skip_migration]
    m.migration_template 'migration.rb',
                         'db/migrate',
                         :assigns => { :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" },
                         :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
  end
end

#attachmentsObject



41
42
43
44
45
46
47
48
49
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 41

def attachments
  if view_param
    parse_attachments_from_param
  elsif view_only?
    model.attachments
  else
    []
  end
end

#is_model_dependency_action(action) ⇒ Object



37
38
39
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 37

def is_model_dependency_action(action)
  action[0] == :dependency && action[1].include?('model')
end

#manifestObject



12
13
14
15
16
17
18
19
20
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 12

def manifest
  m = super.edit do |action|
    action unless is_model_dependency_action(action) || !valid
  end
  unless view_only? || !valid
    add_model_actions(m)
  end
  m
end

#paperclip_installedObject



89
90
91
92
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 89

def paperclip_installed
  ActiveRecord::Base.methods.include?('has_attached_file') ||
  ActiveRecord::Base.methods.include?(:has_attached_file)
end

#parse_attachments_from_paramObject



51
52
53
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 51

def parse_attachments_from_param
  view_param.split(',')
end

#source_roots_for_viewObject



8
9
10
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 8

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

#validateObject



55
56
57
58
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 55

def validate
  @valid = validate_attachments
  @valid &&= super
end

#validate_attachment(attachment) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 76

def validate_attachment(attachment)
  if view_only?
    if !model.has_attachment?(attachment)
      logger.error "Attachment '#{attachment}' does not exist."
      return false
    elsif !model.has_columns_for_attachment?(attachment)
      logger.error model.error
      return false
    end
  end
  true
end

#validate_attachmentsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/view_mapper/views/paperclip/paperclip_view.rb', line 60

def validate_attachments
  if !paperclip_installed
    logger.error "The Paperclip plugin does not appear to be installed."
    return false
  elsif attachments.empty?
    if view_only?
      logger.warning "No paperclip attachments exist on the specified class."
    else
      logger.warning "No paperclip attachments specified."
    end
    return false
  else
    !attachments.detect { |a| !validate_attachment(a) }
  end
end