Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ActiveStorage

 ActiveStorage

Radoslav Stankov

March 13, 2018
Tweet

More Decks by Radoslav Stankov

Other Decks in Technology

Transcript

  1. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true end
  2. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true end
  3. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true end !
  4. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true end https://rubygems.org/gems/annotate
  5. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true end
  6. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true has_one_attached :photo end
  7. # == Schema Information # # Table name: speakers #

    # id :integer not null, primary # name :string(255) not null # bio :text # created_at :datetime # updated_at :datetime # class Speaker < ApplicationRecord validates :name, presence: true has_one_attached :photo end
  8. create_table 'active_storage_attachments' 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 end 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 end
  9. ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.8ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE

    "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 35], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 30], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.6ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 40], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Blob Load (1.3ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 16], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.0ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 23], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 18], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (2.0ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 49], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (2.0ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 7], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 21], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 11], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 10], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 50], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.0ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 52], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 17], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]] ↳ app/views/admin/speakers/index.html.slim:17 ActiveStorage::Attachment Load (1.8ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 38], ["record_type", "Speaker"], ["name", "photo"], ["LIMIT", 1]]
  10. module HasVariants def has_one_attached_with_variants(attribute_name, variants = {}) has_one_attached attribute_name define_method

    "#{attribute_name}_variant" do |variant_name| attachment = public_send(attribute_name) return unless attachment.attached? variant_options = variants.fetch(variant_name) do raise "UnknownVariant '#{variant_name}', not in #{variants.keys.join})" end attachment.variant(*variant_options) end end end #
  11. class Speaker < ApplicationRecord extend HasVariants has_one_attached_with_variants :photo, { small:

    { resize: '100x100' }, medium: { resize: '200x200' } # ... } end speaker.photo_vartiant(:small) #
  12. $ Disk Service % AWS Service & Azure Service '

    Google Cloud Service ( Mirror Service Services