Skip to content

Commit 94a0128

Browse files
committed
#5 Refactor Photo as Media
1 parent 787f7b2 commit 94a0128

File tree

15 files changed

+38
-50
lines changed

15 files changed

+38
-50
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
kebapage (0.0.1)
4+
kebapage (1.0.0)
55
bootstrap-wysihtml5-rails
66
dropzonejs-rails (= 0.4.12)
77
friendly_id

app/controllers/kebapage/photos_controller.rb app/controllers/kebapage/media_controller.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
require_dependency "kebapage/application_controller"
22

33
module Kebapage
4-
class PhotosController < ApplicationController
4+
class MediaController < ApplicationController
55
def create
6-
@photo = Photo.create(photo: params[:photo])
7-
render json: @photo
6+
@medium = Medium.create(attachment: params[:attachment ])
7+
puts #{params[:attachment]}
8+
render json: @media
89
end
910

1011
def destroy
11-
@photo = Photo.find(params[:id])
12-
@photo.destroy
12+
@medium = Medium.find(params[:id])
13+
@medium.destroy
1314
render nothing: true
1415
end
1516

1617
def photos
17-
@uploads = Photo.all
18+
@uploads = Medium.all
1819

1920
respond_to do |format|
2021
format.json { render json: @uploads }

app/controllers/kebapage/static_pages_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def show
1919

2020
def new
2121
@static_page = StaticPage.new
22-
@photo = Photo.new
22+
@medium = Medium.new
2323
end
2424

2525
def edit
26-
@photo = Photo.new
26+
@medium = Medium.new
2727
end
2828

2929
def create
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module Kebapage
2-
module PhotosHelper
2+
module MediaHelper
33
end
44
end

app/models/kebapage/medium.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Kebapage
2+
class Medium < ActiveRecord::Base
3+
has_attached_file :attachment
4+
end
5+
end

app/models/kebapage/photo.rb

-6
This file was deleted.

app/models/kebapage/static_page.rb

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module Kebapage
44
class StaticPage < ActiveRecord::Base
5-
has_many :photos
65
validates :title, :content, presence: true
76
extend FriendlyId
87
friendly_id :title, use: [:slugged, :history]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<% @uploads.each do |photo| %>
2-
document.wysi.data("wysihtml5").editor.composer.setValue(document.wysi.val() + '<img src="<%= URI.join(request.url, photo.photo.url) %>" />');
2+
document.wysi.data("wysihtml5").editor.composer.setValue(document.wysi.val() + '<img src="<%= URI.join(request.url, photo.attachment.url) %>" />');
33
<% end %>

app/views/kebapage/static_pages/_form.html.haml

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@
1919
%button.btn.btn-default{type: 'submit'} #{I18n.t('kebapage.submit')}
2020
%a.btn{href: static_pages_path} #{I18n.t('kebapage.cancel')}
2121

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

2525
=javascript_include_tag "kebapage/application"
2626
:javascript
2727
Dropzone.options.photoDropzone = {
28-
paramName: "photo",
28+
paramName: "attachment",
2929
maxFilesize: 2,
3030
clickable: true,
3131
addRemoveLinks: true,
3232
init: function () {
33-
this.on("complete", function (file) {
34-
return $.ajax({
35-
url: "#{photos_photos_url(format: :js)}"
36-
});
37-
});
3833
return this.on('removedfile', function(file) {
3934
if (file.xhr) {
4035
return $.ajax({
41-
url: "" + ($("#photo-dropzone").attr("action")) + "/" + (JSON.parse(file.xhr.response).id),
36+
url: "" + ($("#media-dropzone").attr("action")) + "/" + (JSON.parse(file.xhr.response).id),
4237
type: 'DELETE'
4338
});
4439
}

config/routes.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Kebapage::Engine.routes.draw do
2-
resources :photos, only: [:create, :destroy, :photos] do
3-
get :photos, on: :collection
2+
resources :media, only: [:create, :destroy, :photos] do
3+
get :media, on: :collection
44
end
55
resources :static_pages, except: [:show]
66
root 'static_pages#index'

db/migrate/20140119094017_create_kebapage_photos.rb

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CreateKebapageMedia < ActiveRecord::Migration
2+
def change
3+
create_table :kebapage_media do |t|
4+
t.attachment :attachment
5+
end
6+
end
7+
end

lib/generators/kebapage/install/install_generator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def self.next_migration_number(dirname)
2121

2222
def create_migration_file
2323
migration_template 'migrations/static_pages.rb', 'db/migrate/create_kebapage_static_pages.rb' rescue nil
24-
migration_template 'migrations/photos.rb', 'db/migrate/create_kebapage_photos.rb' rescue nil
24+
migration_template 'migrations/media.rb', 'db/migrate/create_kebapage_media.rb' rescue nil
2525
migration_template 'migrations/add_slug_to_kebapage_static_pages.rb', 'db/migrate/add_slug_to_kebapage_static_pages.rb' rescue nil
2626
migration_template 'migrations/friendly_id_slugs.rb', 'db/migrate/create_friendly_id_slugs.rb' rescue nil
2727
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CreateKebapageMedia < ActiveRecord::Migration
2+
def change
3+
create_table :kebapage_media do |t|
4+
t.attachment :attachment
5+
end
6+
end
7+
end

lib/generators/kebapage/install/templates/migrations/photos.rb

-10
This file was deleted.

0 commit comments

Comments
 (0)