Skip to content

Commit

Permalink
Updated file upload forms and refactored placeholder functionality in…
Browse files Browse the repository at this point in the history
…to view.
  • Loading branch information
solidstatejake committed Jun 22, 2019
1 parent 46e0634 commit 839c825
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 40 deletions.
15 changes: 15 additions & 0 deletions app/helpers/portfolios_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
module PortfoliosHelper

def image_generator(height:, width:)
"http://placehold.it/#{height}x#{width}"
end

def portfolio_img img, type
if img.model.main_image? || img.model.thumb_image?
img.to_s
elsif type == 'thumb'
image_generator(height: '350', width: '200')
elsif type == 'main'
image_generator(height: '600', width: '400')
end

end
end
9 changes: 0 additions & 9 deletions app/models/concerns/placeholder.rb

This file was deleted.

17 changes: 3 additions & 14 deletions app/models/portfolio.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
class Portfolio < ApplicationRecord
has_many :technologies
accepts_nested_attributes_for :technologies,
reject_if: lambda {
|attributes| attributes['name'].blank?
}
reject_if: lambda { |attributes| attributes['name'].blank? }

include Placeholder
validates_presence_of :title, :body, :main_image, :thumb_image
validates_presence_of :title, :body

mount_uploader :thumb_image, PortfolioUploader
mount_uploader :main_image, PortfolioUploader
Expand All @@ -19,13 +16,5 @@ def self.by_position
order('position ASC')
end

scope :ruby_on_rails_portfolio_items, -> { where(subtitle: 'Ruby on Rails') }

after_initialize :set_defaults

def set_defaults
self.main_image ||= Placeholder.image_generator(height: '600', width: '400')
self.thumb_image ||= Placeholder.image_generator(height: '350', width: '200')

end
scope :ruby_on_rails_portfolio_items, -> {where(subtitle: 'Ruby on Rails')}
end
7 changes: 0 additions & 7 deletions app/models/skill.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
class Skill < ApplicationRecord
include Placeholder

validates_presence_of :title, :percent_utilized

after_initialize :set_defaults

def set_defaults
self.badge ||= Placeholder.image_generator(height: '250', width: '250')

end
end
1 change: 0 additions & 1 deletion app/uploaders/portfolio_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class PortfolioUploader < CarrierWave::Uploader::Base

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
# FIND IMG FILES IN PUBLIC/UPLOADS....
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
Expand Down
14 changes: 9 additions & 5 deletions app/views/portfolios/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
<%= form.text_field :subtitle, class: 'form-control' %>
</div>

<div class="form-group">
<div class="form-group btn btn-primary">
<label>
Upload a thumb image file <%= form.file_field :thumb_image, style: 'display:none' %>
</label>
</div>

<%= form.file_field :main_image %>
<div class="form-group btn btn-primary">
<label>
Upload a main image file<%= form.file_field :main_image, style: 'display:none' %>
</label>

</div>

<div class="form-group">
<%= form.file_field :thumb_image %>
</div>

</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/portfolios/_portfolio_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="card" data-id="<%= portfolio_item.id %>">
<%= image_tag portfolio_item.thumb_image.to_s, class: 'img-thumbnail' unless portfolio_item.thumb_image.nil? %>
<%= image_tag portfolio_img(portfolio_item.thumb_image, 'thumb') %>


<h2><%= link_to portfolio_item.title, portfolio_show_path(portfolio_item), class: 'stretched-link'%></h2>
Expand Down
6 changes: 3 additions & 3 deletions app/views/portfolios/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<article class="container portfolio-container shadow">

<%= link_to 'Edit Portfolio Item'.titleize, edit_portfolio_path, %>
<%= link_to 'Edit Portfolio Item'.titleize, edit_portfolio_path %>
<div class="row">

<div class="col-md-7">
<%= image_tag @portfolio_item.main_image.to_s, class: 'w-100 d-block mx-auto img-fluid border border-light
shadow' %>
<%= image_tag portfolio_img(@portfolio_item.main_image, 'main'),
class: 'w-100 d-block mx-auto img-fluid border border-light shadow' %>
</div>

<div class="col-md-5 mt-4 portfolio-show-data">
Expand Down

0 comments on commit 839c825

Please sign in to comment.