Class: Graphic

Inherits:
Content
  • Object
show all
Defined in:
app/models/graphic.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Content

#action_allowed?, active, #after_add_callback, all_subclasses, #can_display_in?, #cannot_be_own_parent, display_name, #end_time=, expired, filter_all_content, future, #has_children?, #is_active?, #is_approved?, #is_denied?, #is_expired?, #is_orphan?, #is_pending?, #perform_action, #start_time=

Instance Attribute Details

#fieldObject

Placeholder attributes for rendering.



103
104
105
# File 'app/models/graphic.rb', line 103

def field
  @field
end

#screenObject

Placeholder attributes for rendering.



103
104
105
# File 'app/models/graphic.rb', line 103

def screen
  @screen
end

Class Method Details

.form_attributesObject

Graphics also accept media attributes for the uploaded file.



117
118
119
120
# File 'app/models/graphic.rb', line 117

def self.form_attributes
  attributes = super()
  attributes.concat([{:media_attributes => [:file, :key, :id]}])
end

.preview(data) ⇒ Object

return the cleaned input data



123
124
125
# File 'app/models/graphic.rb', line 123

def self.preview(data)
  data
end

Instance Method Details

#convert_mediaObject

Convert the media if it is supported by the converter.



24
25
26
27
28
# File 'app/models/graphic.rb', line 24

def convert_media
  if self.media.size > 0 && Concerto::ContentConverter.supported_types.include?(self.media[0].file_type)
    self.media = Concerto::ContentConverter.convert(self.media)
  end
end

#pre_render(screen, field) ⇒ Object

Store the information needed when generating the path to the image.



106
107
108
109
# File 'app/models/graphic.rb', line 106

def pre_render(screen, field)
  self.screen = screen
  self.field = field
end

#render(options = {}) ⇒ Object

Responsible for display transformations on an image. Resizes the image to fit a width and height specified (both required ATM). Returns a new (unsaved) Media instance OR a hash with text to render



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/graphic.rb', line 40

def render(options={})
  cache_key = options
  cache_key[:content_id] = self.id
  begin
    image_hash = Rails.cache.read(cache_key)
  rescue Exception => e
    image_hash = nil
    Rails.logger.info("Cache read triggered error - #{e.message}")
  end
  if !image_hash.nil?
    Rails.logger.debug('Cache hit!')
    file = Media.new(
      :attachable => self,
      :file_data => image_hash[:data],
      :file_type => image_hash[:type],
      :file_name => image_hash[:name]
    )
    return file
  end
  Rails.logger.debug('Cache miss!')

  # If we are rendering a preview, we prefer a preview file, either a real one
  # that has been saved or one that if just associated with the model unsaved.
  if options[:id] == 'preview' && !self.media.preview.empty? && params[:id]
    preferred_media = self.media.preview.first
  elsif options[:id] == 'preview' && self.media.any?{ |m| m.key == 'preview' }
    preferred_media = self.media.detect {|m| m.key == 'preview'}
  else
    preferred_media = self.media.preferred.first
  end
  file = preferred_media

  options[:crop] ||= false

  if options.key?(:width) || options.key?(:height)

    if options.key?(:width) && options.key?(:height) &&
       options[:height].to_f == 0 && options[:width].to_f == 0
      return {:status => 400, :text => I18n.t(:bad_request), :content_type => Mime::TEXT}
    end

    require 'concerto_image_magick'
    image = ConcertoImageMagick.graphic_transform(preferred_media, options)
    
    file = Media.new(
      :attachable => self,
      :file_data => image.to_blob,
      :file_type => image.mime_type,
      :file_name => preferred_media.file_name
    )
  end

  cache_data = {:data => file.file_contents, :type => file.file_type, :name => file.file_name}
  begin
    Rails.cache.write(cache_key, cache_data, :expires_in => 2.hours, :race_condition_ttl => 1.minute)
  rescue Exception => e
    Rails.logger.info("Cache write triggered error - #{e.message}")
  end

  return file
end

#render_detailsObject

Generate the path to the iamge to be displayed.



112
113
114
# File 'app/models/graphic.rb', line 112

def render_details
  {:path => "#{Rails.application.config.relative_url_root}#{url_helpers.frontend_screen_field_content_path(self.screen, self.field, self)}"}
end

#set_kindObject

Automatically set the kind for the content if it is new.



32
33
34
35
# File 'app/models/graphic.rb', line 32

def set_kind
  return unless new_record?
  self.kind = Kind.where(:name => 'Graphics').first
end