Class: Lines::Admin::ArticlesController

Inherits:
Lines::ApplicationController show all
Defined in:
app/controllers/lines/admin/articles_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/articles



54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/lines/admin/articles_controller.rb', line 54

def create
  @article = Lines::Article.new(article_params)

  respond_to do |format|
    if @article.save
      format.html { redirect_to admin_article_path(@article) }
    else
      format.html { render action: "new" }
    end
  end
end

#destroyObject

DELETE /admin/articles/1



93
94
95
96
97
98
99
100
# File 'app/controllers/lines/admin/articles_controller.rb', line 93

def destroy
  @article = Article.friendly.find(params[:id])
  @article.destroy

  respond_to do |format|
    format.html { redirect_to admin_articles_url }
  end
end

#editObject

GET /admin/articles/1/edit



49
50
51
# File 'app/controllers/lines/admin/articles_controller.rb', line 49

def edit
  @article = Lines::Article.friendly.find(params[:id])
end

#indexObject

Lists all articles. Provides @articles_unpublished and @articles_published to distinguish between published and unpublished articles



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

def index
  @articles = Lines::Article.order('published ASC, published_at DESC, created_at DESC').page(params[:page]).per(25)
  @articles_unpublished = @articles.select{|a| a.published == false}
  @articles_published = @articles.select{|a| a.published == true}
  respond_to do |format|
    format.html # index.html.erb
  end
end

#newObject

GET /admin/articles/new



40
41
42
43
44
45
46
# File 'app/controllers/lines/admin/articles_controller.rb', line 40

def new
  @article = Lines::Article.new

  respond_to do |format|
    format.html # new.html.erb
  end
end

#process_base64_uploadObject

Handles base64 encoded file uploads



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
# File 'app/controllers/lines/admin/articles_controller.rb', line 124

def process_base64_upload
  @uploaded_file = nil
  #check if file is given
  if params[:article][:hero_image_file] != ""

    picture_filename = "hero_image"
    picture_original_filename = "hero_image"
    raise params[:article][:hero_image_file].inspect
    picture_content_type = splitBase64(params[:article][:hero_image_file])[:type]
    picture_data = splitBase64(params[:article][:hero_image_file])[:data]

    #create a new tempfile named fileupload
    tempfile = Tempfile.new("fileupload")
    tempfile.binmode

    #get the file and decode it with base64 then write it to the tempfile
    tempfile.write(Base64.decode64(picture_data))

    #create a new uploaded file
    @uploaded_file = ActionDispatch::Http::UploadedFile.new(
      tempfile: tempfile, 
      filename: picture_filename,
      original_filename: picture_original_filename
    )
    @uploaded_file.content_type = picture_content_type
  end
end

#showObject

GET /admin/articles/1



30
31
32
33
34
35
36
37
# File 'app/controllers/lines/admin/articles_controller.rb', line 30

def show
  @article = Lines::Article.friendly.find(params[:id])
  @first_page = true

  respond_to do |format|
    format.html {render :show, layout: 'lines/preview'}
  end
end

#splitBase64(uri) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/lines/admin/articles_controller.rb', line 152

def splitBase64(uri)
  if uri.match(%r{^data:(.*?);(.*?),(.*)$})
    return {
      type:      $1, # "image/png"
      encoder:   $2, # "base64"
      data:      $3, # data string
      extension: $1.split('/')[1] # "png"
      }
  end
end

#toggle_featureObject

Toggles featured state of an article



111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/lines/admin/articles_controller.rb', line 111

def toggle_feature
  @article = Article.friendly.find(params[:article_id])
  old_featured = Article.where(featured: true)
  if old_featured.size > 0
    old_featured.each do |article|
      article.update_attributes(featured: false)
    end
  end
  @article.update_attributes(featured: !@article.featured)
  redirect_to admin_articles_url
end

#toggle_publishObject

Toggles published state of an article



103
104
105
106
107
108
# File 'app/controllers/lines/admin/articles_controller.rb', line 103

def toggle_publish
  @article = Article.friendly.find(params[:article_id])
  @article.update_attributes(published: !@article.published)
  flash[:success] = "#{@article.title}” has been #{'un' if !@article.published}published."
  redirect_to admin_articles_url
end

#updateObject

PUT /admin/articles/1 TODO: Very much is happening here. Move deletion of hero_image to the article model



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/lines/admin/articles_controller.rb', line 68

def update
  @article = Lines::Article.friendly.find(params[:id])
  a_params = article_params

  # replace picture_path with the new uploaded file
  a_params[:hero_image] = @uploaded_file if @uploaded_file

  # delete uploaded hero image when predifined image is selected
  if !a_params[:hero_image_cache].present? && a_params[:short_hero_image].present?
    @article.remove_hero_image! 
    @article.remove_hero_image = true 
    @article.save
  end

  respond_to do |format|
    if @article.update_attributes(article_params)
      ActionController::Base.new.expire_fragment(@article)
      format.html { redirect_to admin_article_path(@article) }
    else
      format.html { render action: "edit" }
    end
  end
end