Class: TemplatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/templates_controller.rb

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#after_sign_in_path_for, #allow_cors, #auth!, #check_for_initial_install, #compute_pending_moderation, #current_ability, #current_accessor, #current_screen, #http_basic_user_name_and_password, #precompile_error_catch, #process_notification, #restart_webserver, #screen_api, #set_locale, #set_time_zone, #set_version, #sign_in_screen, #sign_out_screen, #switch_to_main_app_ability, #switch_to_plugin_ability, #use_plugin_ability, #webserver_supports_restart?

Instance Method Details

- (Object) create

POST /templates POST /templates.xml



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/templates_controller.rb', line 51

def create
  @template = Template.new(template_params)
  auth!
  # set key based on file extension
  @template.media.each do |media|
    extension = (media.file_name.blank? ? nil : media.file_name.split('.')[-1].downcase)
    media.key = (extension == "css" ? "css" : "original") unless extension.nil?
  end

  # reject any empty media (key wont be set)
  @template.media.reject! {|i| i.key.blank?}

  respond_to do |format|
    if @template.save
      process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
      format.html { redirect_to(edit_template_path(@template), :notice => t(:template_created)) }
      format.xml  { render :xml => @template, :status => :created, :location => @template }
    else
      @type = "create"
      format.html { render :new }
      format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
    end
  end
end

- (Object) destroy

DELETE /templates/1 DELETE /templates/1.xml



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/templates_controller.rb', line 97

def destroy
  @template = Template.find(params[:id])
  auth!

  unless @template.is_deletable?
    redirect_to(@template, :notice => t(:cannot_delete_template, :screens => @template.screens.collect { |s| s.name if can? :read, s}.join(", ")))
    return
  end

  process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
  @template.destroy
  respond_with(@template)
end

- (Object) edit

GET /templates/1/edit



41
42
43
44
45
46
47
# File 'app/controllers/templates_controller.rb', line 41

def edit
  @template = Template.find(params[:id])
  auth!
  while @template.media.length < 2 do
    @template.media.build
  end
end

- (Object) import

PUT /templates/import PUT /templates/import.xml Import a template from an XML description and convert it to an actual template model.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/templates_controller.rb', line 174

def import
  @template = Template.new
  archive = params[:package]

  if @template.import_archive(archive) 
    # is_hidden checkbox supercedes xml
    @template.is_hidden = template_params[:is_hidden]
    if @template.save
      process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
      flash[:notice] = t(:template_created)
    end
  end

  respond_with(@template)
end

- (Object) index

GET /templates GET /templates.xml GET /templates.js



11
12
13
14
15
# File 'app/controllers/templates_controller.rb', line 11

def index
  @templates = Template.all
  auth!
  respond_with(@templates)
end

- (Object) new

GET /templates/new GET /templates/new.xml



31
32
33
34
35
36
37
38
# File 'app/controllers/templates_controller.rb', line 31

def new
  @template = Template.new
  auth!
  # one for the graphic background and one for the css file
  @template.media.build()
  @template.media.build()
  respond_with(@template)
end

- (Object) preview

GET /template/1/preview Generate a preview of the template based on the request format.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/templates_controller.rb', line 113

def preview
  @template = Template.find(params[:id])
  auth!(:action => :read)

  if stale?(:last_modified => @template.last_modified.utc, :etag => @template, :public => true)
    # Hide the fields if the hide_fields param is set,
    # show them by default though.
    @hide_fields = false
    if !params[:hide_fields].nil?
      @hide_fields = [true, "true", 1, "1"].include?(params[:hide_fields])
    end

    # Hide the field names if the hide_text param is set,
    # show them by default though.
    @hide_text = false
    if !params[:hide_text].nil?
      @hide_text = [true, "true", 1, "1"].include?(params[:hide_text])
    end

    @only_fields = []
    if !params[:fields].nil?
      @only_fields = params[:fields].split(',').map{|i| i.to_i}
    end

    jpg =  Mime::Type.lookup_by_extension(:jpg)  #JPG is getting defined elsewhere.
    if([jpg, Mime::PNG, Mime::HTML].include?(request.format))
      @image = nil
      @image = @template.preview_image(@hide_fields, @hide_text, @only_fields)

      # Resize the image if needed.
      # We do this post-field drawing because RMagick seems to struggle with small font sizes.
      if  !params[:height].nil? || !params[:width].nil?
        require 'concerto_image_magick'
        @image = ConcertoImageMagick.resize(@image, params[:width].to_i, params[:height].to_i)
      end

      case request.format
        when jpg
          @image.format = "JPG"
        when Mime::PNG
          @image.format = "PNG"
      end

      data = nil
      data = @image.to_blob

      send_data data,
                :filename => "#{@template.name.underscore}.#{@image.format.downcase}_preview",
                :type => @image.mime_type, :disposition => 'inline'
    else
      respond_to do |format|
        format.svg
      end
    end
  end
end

- (Object) show

GET /templates/1 GET /templates/1.xml GET /templates/1.js



20
21
22
23
24
25
26
27
# File 'app/controllers/templates_controller.rb', line 20

def show
  @template = Template.find(params[:id])
  run_callbacks :show # Run plugin hooks
  auth!
  respond_with(@template) do |format|
    format.xml { render :xml => @template.to_xml(:include => [:positions])  }
  end
end

- (Object) update

PUT /templates/1 PUT /templates/1.xml



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/templates_controller.rb', line 78

def update
  @template = Template.find(params[:id])
  auth!
  # don't stomp on the key, set elsewhere
  # @template.media.each do |media|
  #   media.key = "original"
  # end

  if @template.update_attributes(template_params)
    process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
    flash[:notice] = t(:template_updated)
  end

  respond_with(@template)

end