Skip to content

Commit

Permalink
Install Trix editor, and create activities#edit and activities#update…
Browse files Browse the repository at this point in the history
…, and other misc edits.

* Set default date value in form
* Made form style updates
* Created helper to format duration
  • Loading branch information
Steve Polito committed Aug 15, 2020
1 parent 7f32eca commit 4922549
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 5 deletions.
36 changes: 36 additions & 0 deletions app/assets/stylesheets/actiontext.scss
@@ -0,0 +1,36 @@
//
// Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
// the trix-editor content (whether displayed or under editing). Feel free to incorporate this
// inclusion directly in any other asset bundle and remove this file.
//
//= require trix/dist/trix

// We need to override trix.css’s image gallery styles to accommodate the
// <action-text-attachment> element we wrap around attachments. Otherwise,
// images in galleries will be squished by the max-width: 33%; rule.
.trix-content {
.attachment-gallery {
> action-text-attachment,
> .attachment {
flex: 1 0 33%;
padding: 0 0.5em;
max-width: 33%;
}

&.attachment-gallery--2,
&.attachment-gallery--4 {
> action-text-attachment,
> .attachment {
flex-basis: 50%;
max-width: 50%;
}
}
}

action-text-attachment {
.attachment {
padding: 0 !important;
max-width: 100% !important;
}
}
}
15 changes: 13 additions & 2 deletions app/controllers/activities_controller.rb
@@ -1,6 +1,6 @@
class ActivitiesController < ApplicationController
before_action :authenticate_user!
before_action :set_activity, only: [:show]
before_action :set_activity, only: [:show, :edit, :update]
before_action :calculate_duration, only: [:create]

def show
Expand All @@ -19,10 +19,21 @@ def create
end
end

def edit
end

def update
if @activity.update(activity_params)
redirect_to @activity, notice: "Updated Activity"
else
render "edit"
end
end

private

def activity_params
params.require(:activity).permit(:duration, :category, :distance, :difficulty, :unit, :date)
params.require(:activity).permit(:duration, :category, :distance, :difficulty, :unit, :date, :description)
end

def set_activity
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/packs/application.js
Expand Up @@ -24,3 +24,6 @@ document.addEventListener("turbolinks:load", ()=>{
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)

require("trix")
require("@rails/actiontext")
2 changes: 2 additions & 0 deletions app/models/activity.rb
Expand Up @@ -6,6 +6,8 @@ class Activity < ApplicationRecord
enum difficulty: [:easy, :moderate, :hard]
enum unit: [:miles, :kilometers, :meters, :yards]

has_rich_text :description

before_save :calculate_pace

validates :date, presence: true
Expand Down
14 changes: 14 additions & 0 deletions app/views/active_storage/blobs/_blob.html.erb
@@ -0,0 +1,14 @@
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>

<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
5 changes: 4 additions & 1 deletion app/views/activities/_form.html.erb
Expand Up @@ -29,6 +29,9 @@
<%= f.number_field :seconds, min: 0, max: 59, step: 1, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit %>
<%= f.rich_text_area :description, class: "form-control"%>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/activities/edit.html.erb
@@ -0,0 +1 @@
<%= render "form" %>
@@ -0,0 +1,27 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20200815121510_create_action_text_tables.action_text.rb
@@ -0,0 +1,14 @@
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
create_table :action_text_rich_texts do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false

t.timestamps

t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
end
end
end
34 changes: 33 additions & 1 deletion db/schema.rb
Expand Up @@ -10,11 +10,42 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_08_13_110652) do
ActiveRecord::Schema.define(version: 2020_08_15_121510) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
t.string "record_type", null: false
t.bigint "record_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
end

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "activities", force: :cascade do |t|
t.integer "duration"
t.integer "category", default: 0, null: false
Expand Down Expand Up @@ -47,5 +78,6 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "activities", "users"
end
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -3,13 +3,15 @@
"private": true,
"dependencies": {
"@rails/actioncable": "^6.0.0",
"@rails/actiontext": "^6.0.3-2",
"@rails/activestorage": "^6.0.0",
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "4.2.2",
"and": "^0.0.3",
"bootstrap": "^4.5.1",
"jquery": "^3.5.1",
"popper.js": "^1.16.1",
"trix": "^1.2.0",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/action_text/rich_texts.yml
@@ -0,0 +1,4 @@
# one:
# record: name_of_fixture (ClassOfFixture)
# name: content
# body: <p>In a <i>million</i> stars!</p>
14 changes: 13 additions & 1 deletion yarn.lock
Expand Up @@ -850,7 +850,14 @@
resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-6.0.3.tgz#722b4b639936129307ddbab3a390f6bcacf3e7bc"
integrity sha512-I01hgqxxnOgOtJTGlq0ZsGJYiTEEiSGVEGQn3vimZSqEP1HqzyFNbzGTq14Xdyeow2yGJjygjoFF1pmtE+SQaw==

"@rails/activestorage@^6.0.0":
"@rails/actiontext@^6.0.3-2":
version "6.0.3-2"
resolved "https://registry.yarnpkg.com/@rails/actiontext/-/actiontext-6.0.3-2.tgz#19739f5c58fed879fedcd98039baf2be0bece52d"
integrity sha512-X7e7mDFeQZoTyPn+Dtu+dhwWfr/9I/h+pVhsAKCithCslcefuk5q4vmlfKWyIaKYNF5j821UdR3gfI8Nwr8Wjg==
dependencies:
"@rails/activestorage" "^6.0.0-alpha"

"@rails/activestorage@^6.0.0", "@rails/activestorage@^6.0.0-alpha":
version "6.0.3"
resolved "https://registry.yarnpkg.com/@rails/activestorage/-/activestorage-6.0.3.tgz#401d2a28ecb7167cdb5e830ffddaa17c308c31aa"
integrity sha512-YdNwyfryHlcKj7Ruix89wZ2aiN3KTYULdW1Y/hNlHJlrY2/PXjT2YBTzZiVd+dcjrwHBsXV2rExdy+Z/lsrlEg==
Expand Down Expand Up @@ -7014,6 +7021,11 @@ trim-newlines@^1.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
integrity sha1-WIeWa7WCpFA6QetST301ARgVphM=

trix@^1.2.0:
version "1.2.4"
resolved "https://registry.yarnpkg.com/trix/-/trix-1.2.4.tgz#8b8f8ea58c2605ad3d04a3b82d60a994576e33a5"
integrity sha512-S6YUUaaHngNbW1jzBV3MQ35pisTV/x5yKlNZrAf8exkOnHsTj+2OJHx2jBLqRQEtpWHpg85pHjo2A7dKK2RQbQ==

"true-case-path@^1.0.2":
version "1.0.3"
resolved "https://registry.yarnpkg.com/true-case-path/-/true-case-path-1.0.3.tgz#f813b5a8c86b40da59606722b144e3225799f47d"
Expand Down

0 comments on commit 4922549

Please sign in to comment.