Skip to content

Commit

Permalink
#5 Refactor Photo as Media
Browse files Browse the repository at this point in the history
  • Loading branch information
marjinal1st committed Jan 21, 2014
1 parent 787f7b2 commit 94a0128
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
kebapage (0.0.1)
kebapage (1.0.0)
bootstrap-wysihtml5-rails
dropzonejs-rails (= 0.4.12)
friendly_id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
require_dependency "kebapage/application_controller"

module Kebapage
class PhotosController < ApplicationController
class MediaController < ApplicationController
def create
@photo = Photo.create(photo: params[:photo])
render json: @photo
@medium = Medium.create(attachment: params[:attachment ])
puts #{params[:attachment]}
render json: @media
end

def destroy
@photo = Photo.find(params[:id])
@photo.destroy
@medium = Medium.find(params[:id])
@medium.destroy
render nothing: true
end

def photos
@uploads = Photo.all
@uploads = Medium.all

respond_to do |format|
format.json { render json: @uploads }
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/kebapage/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def show

def new
@static_page = StaticPage.new
@photo = Photo.new
@medium = Medium.new
end

def edit
@photo = Photo.new
@medium = Medium.new
end

def create
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Kebapage
module PhotosHelper
module MediaHelper
end
end
5 changes: 5 additions & 0 deletions app/models/kebapage/medium.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Kebapage
class Medium < ActiveRecord::Base
has_attached_file :attachment
end
end
6 changes: 0 additions & 6 deletions app/models/kebapage/photo.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/kebapage/static_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Kebapage
class StaticPage < ActiveRecord::Base
has_many :photos
validates :title, :content, presence: true
extend FriendlyId
friendly_id :title, use: [:slugged, :history]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<% @uploads.each do |photo| %>
document.wysi.data("wysihtml5").editor.composer.setValue(document.wysi.val() + '<img src="<%= URI.join(request.url, photo.photo.url) %>" />');
document.wysi.data("wysihtml5").editor.composer.setValue(document.wysi.val() + '<img src="<%= URI.join(request.url, photo.attachment.url) %>" />');
<% end %>
13 changes: 4 additions & 9 deletions app/views/kebapage/static_pages/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,21 @@
%button.btn.btn-default{type: 'submit'} #{I18n.t('kebapage.submit')}
%a.btn{href: static_pages_path} #{I18n.t('kebapage.cancel')}

=form_for @photo, multipart: true, html: { class: :dropzone, id: 'photo-dropzone' } do |f|
=f.hidden_field :photo
=form_for @medium, multipart: true, html: { class: :dropzone, id: 'media-dropzone' } do |f|
=f.hidden_field :attachment

=javascript_include_tag "kebapage/application"
:javascript
Dropzone.options.photoDropzone = {
paramName: "photo",
paramName: "attachment",
maxFilesize: 2,
clickable: true,
addRemoveLinks: true,
init: function () {
this.on("complete", function (file) {
return $.ajax({
url: "#{photos_photos_url(format: :js)}"
});
});
return this.on('removedfile', function(file) {
if (file.xhr) {
return $.ajax({
url: "" + ($("#photo-dropzone").attr("action")) + "/" + (JSON.parse(file.xhr.response).id),
url: "" + ($("#media-dropzone").attr("action")) + "/" + (JSON.parse(file.xhr.response).id),
type: 'DELETE'
});
}
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Kebapage::Engine.routes.draw do
resources :photos, only: [:create, :destroy, :photos] do
get :photos, on: :collection
resources :media, only: [:create, :destroy, :photos] do
get :media, on: :collection
end
resources :static_pages, except: [:show]
root 'static_pages#index'
Expand Down
10 changes: 0 additions & 10 deletions db/migrate/20140119094017_create_kebapage_photos.rb

This file was deleted.

7 changes: 7 additions & 0 deletions db/migrate/20140121103448_create_kebapage_media.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateKebapageMedia < ActiveRecord::Migration
def change
create_table :kebapage_media do |t|
t.attachment :attachment
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/kebapage/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.next_migration_number(dirname)

def create_migration_file
migration_template 'migrations/static_pages.rb', 'db/migrate/create_kebapage_static_pages.rb' rescue nil
migration_template 'migrations/photos.rb', 'db/migrate/create_kebapage_photos.rb' rescue nil
migration_template 'migrations/media.rb', 'db/migrate/create_kebapage_media.rb' rescue nil
migration_template 'migrations/add_slug_to_kebapage_static_pages.rb', 'db/migrate/add_slug_to_kebapage_static_pages.rb' rescue nil
migration_template 'migrations/friendly_id_slugs.rb', 'db/migrate/create_friendly_id_slugs.rb' rescue nil
end
Expand Down
7 changes: 7 additions & 0 deletions lib/generators/kebapage/install/templates/migrations/media.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateKebapageMedia < ActiveRecord::Migration
def change
create_table :kebapage_media do |t|
t.attachment :attachment
end
end
end
10 changes: 0 additions & 10 deletions lib/generators/kebapage/install/templates/migrations/photos.rb

This file was deleted.

0 comments on commit 94a0128

Please sign in to comment.