Class: Admin::ArticlesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Admin::ArticlesController
- Defined in:
- app/controllers/lines/admin/articles_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
POST /admin/articles.
-
#destroy ⇒ Object
DELETE /admin/articles/1.
-
#edit ⇒ Object
GET /admin/articles/1/edit.
-
#index ⇒ Object
Lists all articles.
-
#new ⇒ Object
GET /admin/articles/new.
-
#process_base64_upload ⇒ Object
Handles base64 encoded file uploads.
-
#show ⇒ Object
GET /admin/articles/1.
- #splitBase64(uri) ⇒ Object
-
#toggle_feature ⇒ Object
Toggles featured state of an article.
-
#toggle_publish ⇒ Object
Toggles published state of an article.
-
#update ⇒ Object
PUT /admin/articles/1 TODO: Very much is happening here.
Instance Method Details
#create ⇒ Object
POST /admin/articles
53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 53 def create @article = Article.new(article_params) respond_to do |format| if @article.save format.html { redirect_to admin_article_path(@article), notice: 'Article was successfully created.' } else format.html { render action: "new" } end end end |
#destroy ⇒ Object
DELETE /admin/articles/1
92 93 94 95 96 97 98 99 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 92 def destroy @article = Article.find(params[:id]) @article.destroy respond_to do |format| format.html { redirect_to admin_articles_url } end end |
#edit ⇒ Object
GET /admin/articles/1/edit
48 49 50 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 48 def edit @article = Article.find(params[:id]) end |
#index ⇒ Object
Lists all articles. Provides @articles_unpublished
and @articles_published
to distinguish between published and unpublished articles
19 20 21 22 23 24 25 26 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 19 def index @articles = Article.order('published ASC, featured DESC, published_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 |
#new ⇒ Object
GET /admin/articles/new
39 40 41 42 43 44 45 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 39 def new @article = Lines::Article.new respond_to do |format| format.html # new.html.erb end end |
#process_base64_upload ⇒ Object
Handles base64 encoded file uploads
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 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 122 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" 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 |
#show ⇒ Object
GET /admin/articles/1
29 30 31 32 33 34 35 36 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 29 def show @article = Article.find(params[:id]) @first_page = true respond_to do |format| format.html {render :show, layout: 'lines/preview'} end end |
#splitBase64(uri) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 149 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_feature ⇒ Object
Toggles featured state of an article
109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 109 def toggle_feature @article = Article.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, notice: 'Article updated!' end |
#toggle_publish ⇒ Object
Toggles published state of an article
102 103 104 105 106 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 102 def toggle_publish @article = Article.find(params[:article_id]) @article.update_attributes(published: !@article.published) redirect_to admin_articles_url, notice: 'Article updated!' end |
#update ⇒ Object
PUT /admin/articles/1 TODO: Very much is happening here. Move deletion of hero_image to the article model
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/controllers/lines/admin/articles_controller.rb', line 67 def update @article = Article.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), notice: 'Article was successfully updated.' } else format.html { render action: "edit" } end end end |