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

[V2]active storage #52

Open
wants to merge 1 commit into
base: version-2
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ gem "tzinfo-data", platforms: [:windows, :jruby]
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "image_processing", "~> 1.2"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,21 @@ GEM
railties (>= 5.0.0)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
ffi (1.17.0-aarch64-linux-gnu)
ffi (1.17.0-arm-linux-gnu)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86-linux-gnu)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
friendly_id (5.5.1)
activerecord (>= 4.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
image_processing (1.13.0)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inertia_rails (3.2.0)
railties (>= 5)
io-console (0.7.2)
Expand All @@ -155,6 +164,7 @@ GEM
net-smtp
marcel (1.0.4)
matrix (0.4.2)
mini_magick (4.13.2)
mini_mime (1.1.5)
minitest (5.25.1)
msgpack (1.7.2)
Expand Down Expand Up @@ -295,6 +305,9 @@ GEM
ruby-lsp-rspec (0.1.12)
ruby-lsp (~> 0.17.0)
ruby-progressbar (1.13.0)
ruby-vips (2.2.2)
ffi (~> 1.12)
logger
rubyzip (2.3.2)
securerandom (0.3.1)
selenium-webdriver (4.24.0)
Expand Down Expand Up @@ -380,6 +393,7 @@ DEPENDENCIES
factory_bot_rails (~> 6.4)
faker (~> 3.3)
friendly_id (~> 5.5)
image_processing (~> 1.2)
inertia_rails (~> 3.1)
jbuilder
oj_serializers (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def show
private

def company_scope
Company.includes(:technologies, :continent, :country, :region, :city)
Company.includes(:technologies, :continent, :country, :region, :city, :logo_attachment)
end
end
1 change: 1 addition & 0 deletions app/frontend/pages/companies/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function Show({ company }: Props) {

return (
<div className="container mx-auto py-10">
<img src={company.logoUrl} />
<h1 className="my-4 text-xl font-bold">{company.name}</h1>
<div className="flex space-x-4 my-8">
{company.website && (
Expand Down
1 change: 1 addition & 0 deletions app/frontend/types/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Company {
medium: string;
full: string;
};
logoUrl?: string;
continent: GeoFragment;
country: GeoFragment;
region: GeoFragment;
Expand Down
2 changes: 2 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Company < ApplicationRecord
has_one :country, through: :region
has_one :continent, through: :country

has_one_attached :logo

validates :name, presence: true
validates :website, url: { no_local: true, public_suffix: true }
validates :careers_page, url: { no_local: true, public_suffix: true }, allow_blank: true
Expand Down
8 changes: 8 additions & 0 deletions app/serializers/company_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class CompanySerializer < ApplicationSerializer
include Rails.application.routes.url_helpers

attributes(
:id,
:slug,
Expand All @@ -18,6 +20,12 @@ class CompanySerializer < ApplicationSerializer
}
end

attribute :logo_url do
return unless company&.logo&.attached?

rails_representation_url(company.logo.variant(resize_to_fit: [100, 100]).processed, only_path: true)
end

has_many :technologies, serializer: TechnologySerializer
has_one :continent, serializer: GeoFragmentSerializer
has_one :country, serializer: GeoFragmentSerializer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table(:active_storage_blobs, id: primary_key_type) 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)

if connection.supports_datetime_with_precision?
t.datetime(:created_at, precision: 6, null: false)
else
t.datetime(:created_at, null: false)
end

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

create_table(:active_storage_attachments, id: primary_key_type) do |t|
t.string(:name, null: false)
t.references(:record, null: false, polymorphic: true, index: false, type: foreign_key_type)
t.references(:blob, null: false, type: foreign_key_type)

if connection.supports_datetime_with_precision?
t.datetime(:created_at, precision: 6, null: false)
else
t.datetime(:created_at, null: false)
end

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, id: primary_key_type) do |t|
t.belongs_to(:blob, null: false, index: false, type: foreign_key_type)
t.string(:variation_digest, null: false)

t.index([:blob_id, :variation_digest], name: :index_active_storage_variant_records_uniqueness, unique: true)
t.foreign_key(:active_storage_blobs, column: :blob_id)
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
30 changes: 30 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def seed_companies
and Internal Tool Builder for Ruby on Rails that saves engineers and teams months of development time.",
city: @bucharest,
technologies: [@ruby, @rails],
logo: {
io: File.open(Rails.root.join("db/seeds/avohq.png")),
filename: "avohq.png",
content_type: "image/png",
},
)

create_records(Company, 48) do |i|
Expand Down
Binary file added db/seeds/avohq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions spec/controllers/companies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require_factories
require_inertia

let(:company) { create(:company) }

describe "#index", inertia: true do
it "renders a list of companies" do
companies = create_list(:company, 5)
Expand All @@ -19,8 +21,6 @@

describe "#show", inertia: true do
it "renders a company" do
company = create(:company)

get "/companies/#{company.slug}"

expect_inertia.to(render_component("companies/show"))
Expand Down
38 changes: 38 additions & 0 deletions spec/serializers/company_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe(CompanySerializer, type: :serializer) do
require_factories

let(:company) { create(:company) }
subject { CompanySerializer.one(company) }

describe "#logo_url" do
context "when logo is attached" do
let(:logo_url) { "/path/to/logo.png" }

before do
allow(company).to(receive_message_chain(:logo, :attached?).and_return(true))
allow(company).to(receive_message_chain(:logo, :variant, :processed).and_return(
instance_double(ActiveStorage::VariantWithRecord),
))
allow_any_instance_of(CompanySerializer).to(receive(:rails_representation_url).and_return(logo_url))
end

it "returns the correct logo URL" do
expect(subject[:logoUrl]).to(eq(logo_url))
end
end

context "when logo is not attached" do
before do
allow(company).to(receive_message_chain(:logo, :attached?).and_return(false))
end

it "returns nil" do
expect(subject[:logoUrl]).to(be_nil)
end
end
end
end