Skip to content

Commit

Permalink
added photos in views
Browse files Browse the repository at this point in the history
  • Loading branch information
enterteg committed Mar 25, 2016
1 parent 9426983 commit cdf60f8
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 29 deletions.
63 changes: 51 additions & 12 deletions app/assets/stylesheets/posts.sass
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,63 @@
.edit
margin-top: 20px
float: left

.photos
margin: 40px 20px
width: 100%
.image
float: left
position: relative
img
float: left
margin: 10px
a.destroy_photo
color: black
text-decoration: none
text-align: center
line-height: 17px
font-weight: bold
padding: 3px
opacity: 0.5
right: 20px
top: 20px
position: absolute
display: block
width: 15px
height: 15px
background: white
z-index: 10
&:hover
opacity: 1
cursor: pointer
&.mini
margin: 0 auto
max-width: 460px
display: flex
flex-wrap: wrap
item-align: center
justify-content: center
img
margin: 15px
float: left
section
margin: 80px 0px
.post
padding: 35px 0px 25px 45px
outline: 1px dashed rgba(0,0,0,0.2)
max-width: 700px
margin: 50px auto
font-family: 'Raleway'
position: relative
&.sample
padding: 35px 0px 25px 45px
outline: 1px dashed rgba(0,0,0,0.2)
max-width: 650px
&:before
content: ""
position: absolute
top: -1
right: -1
border-width: 0 50px 50px 0
border-style: solid
border-color: rgba(150,0,150,0.1) white
&:before
content: ""
position: absolute
top: -1
right: -1
border-width: 0 50px 50px 0
border-style: solid
border-color: rgba(150,0,150,0.1) white
header
line-height: 1.5
padding: 10px 50px 10px 5px
Expand Down Expand Up @@ -90,9 +127,11 @@ section
#map_form
max-width: 1100px
margin: 0 auto
margin-top: 50px
margin-top: 150p
padding: 30px 20px 0px 20px
#map_photo

margin-top: 150px
position: relative
z-index: 1
img
Expand Down
21 changes: 18 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]

before_action :find_post, only: [:update, :destroy]
before_action :find_post_with_photos, only: [:show, :edit]
def new
@post = Post.new
end
Expand All @@ -21,6 +21,7 @@ def create
end

def show

end
def edit
end
Expand Down Expand Up @@ -51,12 +52,26 @@ def update_form

respond_to do |format|
format.js
end
end
end

def destroy_photo
@photo = Photo.find(params[:id])

if @photo.destroy
redirect_to edit_post_path(@photo.post), notice: 'Photo succesfully deleted'
else
render :edit, notice: 'Cannot destroy photo'
end
end

private
def find_post
@post = Post.find(params[:id])
end
def find_post_with_photos
@post = Post.includes(:photos).find(params[:id])
end
def post_params
params.require(:post).permit(:title,:desc,:category_id, :locX, :locY)
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/public_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class PublicController < ApplicationController
end

def index
@posts = Post.all.limit(6)
@posts = Post.limit(4).includes(:photos)
end

def travels
@posts = Post.travel.limit(3)
@posts = Post.includes(:photos).travel.limit(3)
@pins = Post.travel.pluck(:locX, :locY, :id, :title, :created_at)
@controller = 'travels'
end

def food
@posts = Post.food.limit(6)
@posts = Post.includes(:photos).food.limit(4)
@controller = 'food'
end

def life
@posts = Post.life.limit(6)
@posts = Post.includes(:photos).life.limit(4)
@controller = 'life'
end

Expand Down
10 changes: 5 additions & 5 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Photo < ActiveRecord::Base
belongs_to :post
has_attached_file :photo, :styles => { :medium => "300x300>",:thumb => "200x100#" }

scope :three_only, -> {limit(3)}
has_attached_file :photo, :styles => { :medium => "300x300>",:thumb => "250x180#" }

validates_attachment :photo,
:presence => true,
:content_type => { :content_type => /\Aphoto\/.*\Z/ },
:size => { :less_than => 1.megabyte }
validates_attachment :photo, size: { less_than: 1.megabyte }
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
end
1 change: 0 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class Post < ActiveRecord::Base
belongs_to :category
has_many :photos


default_scope { order(created_at: :desc)}

scope :travel, -> { where(category_id: 1)}
Expand Down
12 changes: 10 additions & 2 deletions app/views/posts/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=form_for @post, html: {id: 'new_post'} do |f|
=form_for @post, html: {multipart: true, id: 'new_post'} do |f|
-if @post.errors.any?
%ul.errors
- @post.errors.full_messages.each do |msg|
Expand All @@ -14,7 +14,15 @@
= f.label :desc, 'Content'
= f.text_area :desc, class: "tinymce"

= file_field_tag "photos[]", type: :file, multiple: true, name: 'file', class: 'dropzone'
= file_field_tag "photos[]", type: :file, multiple: true
-if @post.photos.any?
.photos
-@post.photos.each do |photo|
.image
= image_tag photo.photo.url(:thumb), id: "photo_#{photo.id}"
= link_to 'X', destroy_photo_path(photo.id), method: :delete, data: { confirm: 'Usunąć zdjęcie?' },class: 'destroy_photo'
.clear
.clear
.submit
= f.submit
= f.number_field :locX, type: 'hidden'
Expand Down
6 changes: 6 additions & 0 deletions app/views/posts/_post.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
%header.sample= post.title
%footer.sample= post.created_at.strftime('%d/%m/%Y')
%article.samlpe_art= post.desc.truncate_words(30).html_safe
-if post.photos.any?
.photos.mini
-post.photos.each_with_index do |photo, i|
- break if i == 4
= image_tag photo.photo.url(:thumb)
.clear
= link_to post do
.button.edit= 'read more...'
- if current_user
Expand Down
6 changes: 6 additions & 0 deletions app/views/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
%header= @post.title
%footer= @post.created_at.strftime('%d/%m/%Y')
%article= @post.desc.html_safe
-if @post.photos.any?
.photos
-@post.photos.each do |photo|
= image_tag photo.photo.url(:thumb)
.clear

= link_to :back do
.button.edit back
- if current_user
Expand Down
1 change: 0 additions & 1 deletion app/views/public/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
%section
- if @controller != 'about'
.header= 'Latests posts'
%hr
-if @posts
= render @posts
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Rails.application.routes.draw do

post 'update_form' => 'posts#update_form'

delete 'destroy_photo/:id' => 'posts#destroy_photo', as: :destroy_photo


post 'login' => 'session#create', as: :login
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cdf60f8

Please sign in to comment.