Class: CmsAdmin::SlidesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/cms_admin/slides_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/cms_admin/slides_controller.rb', line 26

def create
  @slides = []

  # Sometimes params[:file] comes in as a single file object
  unless params[:file].is_a?(Hash)
    uploaded_file = params[:file]
    params[:file] = { }
    params[:file][:file] = [uploaded_file]
  end

  file_array  = params[:file][:file] || [nil]
  label       = params[:file][:label]

  file_array.each_with_index do |file, i|
    file_params = params[:file].merge(:file => file)
    # if file_array.size > 1 && file_params[:label].present?
    #   label = file_params[:label] + " #{i + 1}"
    # end
    @slide = @site.slides.create!(file_params)
    @slides << @slide
  end

  if params[:ajax]
    view = render_to_string(:partial => 'cms_admin/slides/slide', :collection => @slides, :layout => false)
    render :json => {:filelink => @slide.file.url, :view => view.gsub("\n", '')}
  else
    flash[:success] = I18n.t('cms.slides.created')
    redirect_to :action => :index
  end
rescue ActiveRecord::RecordInvalid
  logger.detailed_error($!)
  if params[:ajax]
    render :nothing => true, :status => :unprocessable_entity
  else
    flash.now[:error] = I18n.t('cms.slides.creation_failure')
    render :action => :new
  end
end

#destroyObject



75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/cms_admin/slides_controller.rb', line 75

def destroy
  @slide.destroy
  respond_to do |format|
    format.js
    format.html do
      flash[:success] = I18n.t('cms.slides.deleted')
      redirect_to :action => :index
    end
  end
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/cms_admin/slides_controller.rb', line 8

def index
  @slides = @site.slides.order('cms_slides.position')

    if params[:ajax]
    slides = @slides.images.collect do |slide|
      { :thumb  => slide.file.url(:cms_thumb),
        :image  => slide.file.url }
    end
    render :json => slides
  else
    return redirect_to :action => :new if @site.slides.count == 0
  end
end

#newObject



22
23
24
# File 'app/controllers/cms_admin/slides_controller.rb', line 22

def new
  render
end

#reorderObject



86
87
88
89
90
91
92
93
# File 'app/controllers/cms_admin/slides_controller.rb', line 86

def reorder
  (params[:cms_slide] || []).each_with_index do |id, index|
    if (cms_slide = Cms::Slide.find_by_id(id))
      cms_slide.update_attributes(:position => index)
    end
  end
  render :nothing => true
end

#updateObject



65
66
67
68
69
70
71
72
73
# File 'app/controllers/cms_admin/slides_controller.rb', line 65

def update
  @slide.update_attributes!(params[:file])
  flash[:success] = I18n.t('cms.slides.updated')
  redirect_to :action => :edit, :id => @slide
rescue ActiveRecord::RecordInvalid
  logger.detailed_error($!)
  flash.now[:error] = I18n.t('cms.slides.update_failure')
  render :action => :edit
end