-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from it3s/media
Media
- Loading branch information
Showing
35 changed files
with
549 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
App.components.picture = -> | ||
attributes: -> | ||
saveButton: @container.find('.save-button') | ||
deleteButton: @container.find('.delete-button') | ||
|
||
initialize: -> | ||
@on @attr.saveButton, 'click', @onSave | ||
@on @attr.deleteButton, 'click', @onDelete | ||
|
||
onSave: (evt) -> | ||
evt.preventDefault() | ||
@xhrRequest('update') | ||
|
||
onDelete: (evt) -> | ||
evt.preventDefault() | ||
@xhrRequest('delete') | ||
|
||
xhrRequest: (type) -> | ||
method = if type is 'delete' then 'DELETE' else 'PUT' | ||
action = if type is 'delete' then 'deleted' else 'updated' | ||
$.ajax | ||
type: method | ||
url: @attr.url | ||
dataType: "json" | ||
data: {picture: {description: @container.find('#description').val()}} | ||
success: (data) => | ||
App.mediator.publish "picture:#{action}", data | ||
App.utils.flashMessage(data.flash) | ||
App.mediator.publish 'modal:close' |
41 changes: 41 additions & 0 deletions
41
app/assets/javascripts/components/pictureUploader.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
App.components.pictureUploader = -> | ||
_.extend App.components.uploader(), { | ||
|
||
buttonLabel: "<i class='fa fa-plus'></i>#{I18n.uploader.picture}" | ||
|
||
picturesList: $('.pictures-thumbs') | ||
|
||
pictureThumb: """ | ||
<li data-picture_id="<%= id %>" > | ||
<a href='<%= show_url %>' class='thumb' data-components='modal' data-modal-options='{"remote":"true"}'> | ||
<img class='thumb-image' src='<%= image_url %>' ></img> | ||
</a> | ||
</li> | ||
""" | ||
|
||
afterInitialize: -> | ||
App.mediator.subscribe "picture:updated", @onUpdate.bind(this) | ||
App.mediator.subscribe "picture:deleted", @onDelete.bind(this) | ||
|
||
onDone: (e, data) -> | ||
setTimeout( => | ||
console.log data.result | ||
@addPictureThumb data.result | ||
App.utils.flashMessage(data.result.flash) | ||
@reset() | ||
, 200) | ||
|
||
addPictureThumb: (result) -> | ||
picture = $ _.template(@pictureThumb, result) | ||
@picturesList.prepend picture | ||
App.mediator.publish 'components:start', picture | ||
|
||
onUpdate: (evt, data) -> | ||
@picturesList.find("[data-picture_id=#{data.id}]").attr('title', data.description) | ||
|
||
onDelete: (evt, data) -> | ||
picture = @picturesList.find("[data-picture_id=#{data.id}]") | ||
picture.fadeOut -> picture.remove() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,3 +60,4 @@ | |
@import "relations"; | ||
@import "activities"; | ||
@import "notifications"; | ||
@import "pictures"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
.pictures { | ||
|
||
.uploader .upload-button { | ||
@include button; | ||
width: auto; | ||
|
||
i { margin-right: 5px; } | ||
} | ||
|
||
.pictures-thumbs { | ||
margin-top: 10px; | ||
margin-left: -5px; | ||
|
||
li { display: inline-block; } | ||
|
||
.thumb { | ||
display: inline-block; | ||
border-radius: 5px; | ||
padding: 10px; | ||
margin: 5px; | ||
background-color: $meppit-light-blue; | ||
vertical-align: top; | ||
width: 125px; | ||
height: 125px; | ||
|
||
&:hover { | ||
background-color: $meppit-blue; | ||
} | ||
|
||
.thumb-image { | ||
width: 105px; | ||
max-height: 105px; | ||
border-radius: 5px; | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
.picture { | ||
.picture-image { | ||
text-align: center; | ||
|
||
img { | ||
max-width: 400px; | ||
max-height: 400px; | ||
} | ||
} | ||
|
||
.picture-description { | ||
margin-top: 10px; | ||
|
||
textarea { | ||
width: 100%; | ||
margin: 0 auto; | ||
} | ||
} | ||
|
||
.actions { | ||
position: relative; | ||
text-align: center; | ||
margin-top: 10px; | ||
|
||
.delete-button { | ||
@include red-button; | ||
display: inline-block; | ||
margin-right: 40px; | ||
} | ||
.save-button { | ||
@include green-button; | ||
display: inline-block; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class PicturesController < ApplicationController | ||
before_action :require_login, only: [:upload, :update, :destroy] | ||
before_action :find_content | ||
before_action :find_picture, only: [:show, :update, :destroy] | ||
before_action :set_edit_mode, only: [:show] | ||
|
||
def upload | ||
@picture = Picture.new picture_params | ||
|
||
if @picture.valid? && @picture.save | ||
render json: success_json | ||
else | ||
render json: {errors: @picture.errors.messages}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def show | ||
render layout: nil if request.xhr? | ||
end | ||
|
||
def update | ||
@picture.description = params[:picture][:description] unless params[:picture][:description].blank? | ||
@picture.save | ||
render json: success_json | ||
end | ||
|
||
def destroy | ||
@picture.destroy | ||
render json: {id: @picture.id, flash: flash_xhr(t 'flash.deleted')} | ||
end | ||
|
||
private | ||
|
||
def success_json | ||
{ | ||
id: @picture.id, | ||
description: @picture.description, | ||
image_url: @picture.image.url, | ||
show_url: url_for([@content, @picture]), | ||
flash: flash_xhr(t "flash.#{ params[:action] == 'upload' ? 'file_uploaded' : 'saved' }") | ||
} | ||
end | ||
|
||
def picture_params | ||
{ image: params[:picture], author: current_user, content: @content } | ||
end | ||
|
||
def find_content | ||
@content = find_polymorphic_object | ||
end | ||
|
||
def find_picture | ||
@picture = Picture.find params[:id] | ||
end | ||
|
||
def set_edit_mode | ||
@edit_enabled = request.env.fetch('HTTP_REFERER', '').split('/').last == 'edit' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Picture < ActiveRecord::Base | ||
mount_uploader :image, PictureUploader | ||
process_in_background :image | ||
|
||
belongs_to :content, polymorphic: true | ||
belongs_to :author, class_name: 'User' | ||
|
||
|
||
validates :image, presence: true | ||
validates :author, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# encoding: utf-8 | ||
|
||
class PictureUploader < CarrierWave::Uploader::Base | ||
include ::CarrierWave::Backgrounder::Delay | ||
include CarrierWave::MiniMagick | ||
|
||
storage :file | ||
|
||
def store_dir | ||
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | ||
end | ||
|
||
version :thumb do | ||
process :resize_to_fit => [240, 200] | ||
end | ||
|
||
def extension_white_list | ||
%w(jpg jpeg png) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<section class="pictures"> | ||
|
||
<% if object.pictures.count > 0 %> | ||
<%= render 'pictures/thumbs_list', object: object %> | ||
<% else %> | ||
<p class="empty"><%= t 'components.picture.empty', {name: object.try(:name)} %></p> | ||
<% end %> | ||
|
||
</section> |
Oops, something went wrong.