Commit 4af98194deed0d9eb5e3480c82a5ce7f9711066f
Committed by
Rafael Manzo
1 parent
bee9ea0a
Exists in
colab
and in
4 other branches
Refactored Project_controller.rb and project_image.rb
Showing
3 changed files
with
26 additions
and
32 deletions
Show diff stats
app/controllers/projects_controller.rb
| ... | ... | @@ -23,7 +23,7 @@ class ProjectsController < ApplicationController |
| 23 | 23 | @project = Project.new(project_params) |
| 24 | 24 | respond_to do |format| |
| 25 | 25 | create_and_redir(format) |
| 26 | - add_image_url(project_params[:image_url], @project.id) | |
| 26 | + ProjectImage.create(image_url: project_params[:image_url],project_id: @project.id ) | |
| 27 | 27 | end |
| 28 | 28 | end |
| 29 | 29 | |
| ... | ... | @@ -39,12 +39,18 @@ class ProjectsController < ApplicationController |
| 39 | 39 | # GET /projects/1/edit.json |
| 40 | 40 | def edit |
| 41 | 41 | set_project |
| 42 | - check_no_image(@project.id) | |
| 42 | + @project_image = ProjectImage.find_by_project_id(@project.id) | |
| 43 | + if !@project_image.nil? | |
| 44 | + @project_image.check_no_image | |
| 45 | + end | |
| 43 | 46 | end |
| 44 | 47 | |
| 45 | 48 | def update |
| 46 | 49 | set_project |
| 47 | - update_image_url(project_params[:image_url], @project.id) | |
| 50 | + @project_image = ProjectImage.find_by_project_id(@project.id) | |
| 51 | + if !@project_image.nil? | |
| 52 | + @project_image.update(project_params[:image_url]) | |
| 53 | + end | |
| 48 | 54 | if @project.update(project_params) |
| 49 | 55 | redirect_to(project_path(@project.id)) |
| 50 | 56 | else |
| ... | ... | @@ -79,7 +85,6 @@ class ProjectsController < ApplicationController |
| 79 | 85 | def create_and_redir(format) |
| 80 | 86 | if @project.save |
| 81 | 87 | current_user.project_ownerships.create project_id: @project.id |
| 82 | - | |
| 83 | 88 | format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' } |
| 84 | 89 | format.json { render action: 'show', status: :created, location: @project } |
| 85 | 90 | else |
| ... | ... | @@ -87,32 +92,4 @@ class ProjectsController < ApplicationController |
| 87 | 92 | format.json { render json: @project.errors, status: :unprocessable_entity } |
| 88 | 93 | end |
| 89 | 94 | end |
| 90 | - | |
| 91 | - def add_image_url url,project_id | |
| 92 | - if url.blank? | |
| 93 | - url = "no-image-available.png" | |
| 94 | - end | |
| 95 | - ProjectImage.create(image_url: url,project_id: project_id ) | |
| 96 | - end | |
| 97 | - | |
| 98 | - def update_image_url url,project_id | |
| 99 | - if url.blank? | |
| 100 | - url = "no-image-available.png" | |
| 101 | - end | |
| 102 | - @project_image = ProjectImage.find_by_project_id(project_id) | |
| 103 | - if !@project_image.blank? | |
| 104 | - @project_image.update(image_url: url) | |
| 105 | - end | |
| 106 | - end | |
| 107 | - | |
| 108 | - def check_no_image project_id | |
| 109 | - @project_image = ProjectImage.find_by_project_id(project_id) | |
| 110 | - | |
| 111 | - if !@project_image.blank? | |
| 112 | - if @project_image.image_url == "no-image-available.png" | |
| 113 | - @project_image.image_url = "" | |
| 114 | - end | |
| 115 | - end | |
| 116 | - end | |
| 117 | - | |
| 118 | 95 | end | ... | ... |
app/models/project.rb
app/models/project_image.rb
| 1 | 1 | class ProjectImage < ActiveRecord::Base |
| 2 | 2 | |
| 3 | 3 | belongs_to :project |
| 4 | + before_save :check_url | |
| 5 | + | |
| 6 | + def check_no_image | |
| 7 | + if !self.blank? | |
| 8 | + if self.image_url == "no-image-available.png" | |
| 9 | + self.image_url = "" | |
| 10 | + end | |
| 11 | + end | |
| 12 | + end | |
| 13 | + | |
| 14 | + def check_url | |
| 15 | + if self.image_url.blank? | |
| 16 | + self.image_url = "no-image-available.png" | |
| 17 | + end | |
| 18 | + end | |
| 19 | + | |
| 4 | 20 | |
| 5 | 21 | end | ... | ... |