Skip to content

Commit

Permalink
Add Trix editor + file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 3, 2017
1 parent 5d93aab commit a75b377
Show file tree
Hide file tree
Showing 48 changed files with 668 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
.byebug_history
/public/packs
/public/packs-test
/public/uploads
/node_modules
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ gem 'jquery-rails', '~> 4.3.1'
gem 'bootstrap', '~> 4.0.0.beta'
gem 'webpacker', '~> 3.0'
gem 'sidekiq', '~> 5.0'
gem 'foreman', '~> 0.84.0'
gem 'foreman', '~> 0.84.0'

gem 'trix'
gem 'shrine'
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ GEM
devise_masquerade (0.6.1)
devise (>= 2.1.0)
railties (>= 3.0)
down (4.0.1)
erubi (1.6.1)
execjs (2.7.0)
ffi (1.9.18)
Expand Down Expand Up @@ -216,6 +217,8 @@ GEM
selenium-webdriver (3.6.0)
childprocess (~> 0.5)
rubyzip (~> 1.0)
shrine (2.6.1)
down (>= 2.3.6)
sidekiq (5.0.5)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
Expand All @@ -236,6 +239,8 @@ GEM
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.8)
trix (0.11.0)
rails (> 4.1, < 5.2)
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.3)
Expand Down Expand Up @@ -283,9 +288,11 @@ DEPENDENCIES
rails (~> 5.1.4)
sass-rails (~> 5.0)
selenium-webdriver
shrine
sidekiq (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
trix
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
//= require popper
//= require bootstrap
//= require turbolinks
//= require trix
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/photos.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/posts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
45 changes: 45 additions & 0 deletions app/assets/javascripts/trix_uploads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Turn off the default Trix captions
Trix.config.attachments.preview.caption = {
name: false,
size: false
};

function uploadAttachment(attachment) {
// Create our form data to submit
var file = attachment.file;
var form = new FormData;
form.append("Content-Type", file.type);
form.append("photo[image]", file);

// Create our XHR request
var xhr = new XMLHttpRequest;
xhr.open("POST", "/photos.json", true);
xhr.setRequestHeader("X-CSRF-Token", Rails.csrfToken());

// Report file uploads back to Trix
xhr.upload.onprogress = function(event) {
var progress = event.loaded / event.total * 100;
attachment.setUploadProgress(progress);
}

// Tell Trix what url and href to use on successful upload
xhr.onload = function() {
if (xhr.status === 201) {
var data = JSON.parse(xhr.responseText);
return attachment.setAttributes({
url: data.image_url,
href: data.url
})
}
}

return xhr.send(form);
}

// Listen for the Trix attachment event to trigger upload
document.addEventListener("trix-attachment-add", function(event) {
var attachment = event.attachment;
if (attachment.file) {
return uploadAttachment(attachment);
}
});
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//*= require trix

// $navbar-default-bg: #312312;
// $light-orange: #ff8c00;
// $navbar-default-color: $light-orange;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/photos.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Photos controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Posts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
74 changes: 74 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy]

# GET /photos
# GET /photos.json
def index
@photos = Photo.all
end

# GET /photos/1
# GET /photos/1.json
def show
end

# GET /photos/new
def new
@photo = Photo.new
end

# GET /photos/1/edit
def edit
end

# POST /photos
# POST /photos.json
def create
@photo = Photo.new(photo_params)

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render :show, status: :created, location: @photo }
else
format.html { render :new }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /photos/1
# PATCH/PUT /photos/1.json
def update
respond_to do |format|
if @photo.update(photo_params)
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { render :show, status: :ok, location: @photo }
else
format.html { render :edit }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /photos/1
# DELETE /photos/1.json
def destroy
@photo.destroy
respond_to do |format|
format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_photo
@photo = Photo.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:image)
end
end
74 changes: 74 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
@posts = Post.all
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body)
end
end
2 changes: 2 additions & 0 deletions app/helpers/photos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PhotosHelper
end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
2 changes: 2 additions & 0 deletions app/models/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ImageUploader < Shrine
end
3 changes: 3 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Photo < ApplicationRecord
include ImageUploader[:image]
end
2 changes: 2 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Post < ApplicationRecord
end
Loading

0 comments on commit a75b377

Please sign in to comment.