Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active storage #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ gem 'jbuilder', '~> 2.7'
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'image_processing', '~> 1.2'

gem "active_storage_validations", "0.9.8"

gem 'twitter', '~> 6.2.0'
gem 'sentry-raven', '~> 2.11'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_storage_validations (0.9.8)
activejob (>= 5.2.0)
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activeadmin (2.9.0)
arbre (~> 1.2, >= 1.2.1)
formtastic (>= 3.1, < 5.0)
Expand Down Expand Up @@ -170,6 +175,9 @@ GEM
http_parser.rb (0.6.0)
i18n (1.9.1)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inherited_resources (1.13.1)
actionpack (>= 5.2, < 7.1)
has_scope (~> 0.6)
Expand Down Expand Up @@ -213,6 +221,7 @@ GEM
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (1.0.0)
mini_magick (4.11.0)
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.15.0)
Expand Down Expand Up @@ -310,6 +319,8 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.3)
ruby-vips (2.1.4)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
ruby_parser (3.18.1)
sexp_processor (~> 4.16)
Expand Down Expand Up @@ -407,7 +418,9 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_storage_validations (= 0.9.8)
activeadmin (~> 2.9.0)
activesupport (= 6.1.4.4)
bootsnap (>= 1.4.4)
byebug
capybara (>= 3.26)
Expand All @@ -417,6 +430,7 @@ DEPENDENCIES
factory_bot_rails
faker (~> 2.19)
haml-rails (~> 2.0.1)
image_processing (~> 1.2)
jbuilder (~> 2.7)
listen (~> 3.3)
mimemagic!
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/letters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def update
@letter = current_user.letters.find(params[:id])
if @letter.update(letter_params)
flash[:notice] = t('flash.updated_letter_successfully')
redirect_to letter_path(@letter)
else
flash[:error] = 'Letter description cannot be empty. Try Again!'
end
redirect_to letter_path(@letter)
end

def like
Expand All @@ -54,6 +54,6 @@ def destroy
private

def letter_params
params.require(:letter).permit(:description)
params.require(:letter).permit(:description, :image, :remove_image)
end
end
15 changes: 15 additions & 0 deletions app/models/letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ class Letter < ActiveRecord::Base

belongs_to :user
has_many :likes, dependent: :destroy
has_one_attached :image do |attachable|
attachable.variant :display, resize_to_limit: [500, 500]
end
validates_presence_of :description
validates :image, content_type: { in: %w[image/jpeg image/png image/gif], message: 'must be a valid image format' }, size: { less_than: 2.megabytes, message: 'must be less than 2.megabytes' }

attr_accessor :remove_image

after_save :purge_image, if: :remove_image

def like_by?(user)
likes.map(&:user_id).include?(user.id)
end

private

def purge_image
image.purge_later
end

end
6 changes: 5 additions & 1 deletion app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
= f.text_area :description, class: 'reasonText'
.markTest.pull-left
= link_to "Markdown supported", "http://daringfireball.net/projects/markdown/", target: '_blank'
%div{style: 'display: flex; align-items: flex-start; padding: 0.2rem'}
= f.label :image
= f.file_field :image, accept: "image/jpeg,image/gif,image/png"
- if user_signed_in?
= f.submit 'Submit', class: 'btn primary pull-right'
-else
Expand Down Expand Up @@ -44,7 +47,8 @@
- unless letter.user_id == 43
.letterDescription
= markdown(letter.description)
- else
- if letter.image.attached?
= image_tag letter.image
.letterDescription43
= markdown(letter.description)
%br
Expand Down
9 changes: 8 additions & 1 deletion app/views/letters/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
.row
.span8
%h3 I Love Ruby Because ..
%h3 I Love Ruby Because ..
= form_for @letter do |f|
= f.text_area :description, class: 'reasonText'
%div{style: 'display: flex; align-items: flex-start; padding: 0.2rem'}
= f.label :image
= f.file_field :image, accept: "image/jpeg,image/gif,image/png"
- if @letter.image.attached?
%div{style: 'display: flex; align-items: flex-start; padding: 0.2rem'}
= f.check_box :remove_image
= f.label :remove_image
.markTest.pull-left
= link_to "Markdown supported", "http://daringfireball.net/projects/markdown/"
= f.submit 'Update', class: 'btn primary pull-right'
1 change: 1 addition & 0 deletions app/views/letters/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.pull-right.thumbnail.imageWrapper{ style: 'max-height: 50px; max-width: 50px' }
= get_twitter_image(@letter.user.twitter_handle)
= markdown(@letter.description)
= image_tag @letter.image if @letter.image.attached?
%br
%div
%div.pull-left
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.0

config.active_storage.variant_processor = :mini_magick
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.string :service_name, null: false
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

create_table :active_storage_variant_records do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
32 changes: 31 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_08_03_025721) do
ActiveRecord::Schema.define(version: 2022_11_10_052417) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -29,6 +29,34 @@
t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
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.string "service_name", null: false
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 "active_storage_variant_records", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "admin_users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -86,4 +114,6 @@
t.index ["uid"], name: "index_users_on_uid"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
end