Skip to content

Commit

Permalink
Remove timestamps from essences and contents (AlchemyCMS#1809)
Browse files Browse the repository at this point in the history
* Remove timestamps from essence and contents tables

These records always ever get created within an element.
So, this information is duplicated.

* Format attachment and picture model classes

with Rufo

* Touch element after essence has been saved

The content will rarely be cached inside of an already cached element.

* Format attachment and picture spec files

* Touch elements after attachment or picture has been saved

Do not touch contents any more. We removed the timestamps as they only get created in the context of an element and the element gets cached, not the single content.

* Rubocop -a
  • Loading branch information
tvdeyen authored May 5, 2020
1 parent c206a2a commit 34b1721
Show file tree
Hide file tree
Showing 23 changed files with 179 additions and 233 deletions.
6 changes: 4 additions & 2 deletions app/models/alchemy/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Attachment < BaseRecord
include Alchemy::Filetypes
include Alchemy::NameConversions
include Alchemy::Taggable
include Alchemy::ContentTouching
include Alchemy::TouchElements

dragonfly_accessor :file, app: :alchemy_attachments do
after_assign { |f| write_attribute(:file_mime_type, f.mime_type) }
Expand Down Expand Up @@ -55,7 +55,8 @@ def file_types_for_select

validates_presence_of :file
validates_size_of :file, maximum: Config.get(:uploader)["file_size_limit"].megabytes
validates_property :ext, of: :file,
validates_property :ext,
of: :file,
in: allowed_filetypes,
case_sensitive: false,
message: Alchemy.t("not a valid file"),
Expand Down Expand Up @@ -89,6 +90,7 @@ def restricted?
def extension
file_name.split(".").last
end

alias_method :suffix, :extension

# Returns a css class name for kind of file
Expand Down
23 changes: 6 additions & 17 deletions app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Picture < BaseRecord

include Alchemy::NameConversions
include Alchemy::Taggable
include Alchemy::ContentTouching
include Alchemy::TouchElements
include Alchemy::Picture::Transformations
include Alchemy::Picture::Url

Expand Down Expand Up @@ -76,21 +76,10 @@ def allowed_filetypes

stampable stamper_class_name: Alchemy.user_class_name

scope :named, ->(name) {
where("#{table_name}.name LIKE ?", "%#{name}%")
}

scope :recent, -> {
where("#{table_name}.created_at > ?", Time.current - 24.hours).order(:created_at)
}

scope :deletable, -> {
where("#{table_name}.id NOT IN (SELECT picture_id FROM #{EssencePicture.table_name})")
}

scope :without_tag, -> {
left_outer_joins(:taggings).where(gutentag_taggings: {id: nil})
}
scope :named, ->(name) { where("#{table_name}.name LIKE ?", "%#{name}%") }
scope :recent, -> { where("#{table_name}.created_at > ?", Time.current - 24.hours).order(:created_at) }
scope :deletable, -> { where("#{table_name}.id NOT IN (SELECT picture_id FROM #{EssencePicture.table_name})") }
scope :without_tag, -> { left_outer_joins(:taggings).where(gutentag_taggings: { id: nil }) }

# Class methods

Expand Down Expand Up @@ -126,7 +115,7 @@ def search_by(params, query, per_page = nil)

def filtered_by(filter = "")
case filter
when "recent" then recent
when "recent" then recent
when "last_upload" then last_upload
when "without_tag" then without_tag
else
Expand Down
24 changes: 0 additions & 24 deletions app/models/concerns/alchemy/content_touching.rb

This file was deleted.

24 changes: 24 additions & 0 deletions app/models/concerns/alchemy/touch_elements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Alchemy
# If the model has a +elements+ association,
# it updates all their timestamps after save.
#
# Should only be used on bottom to top relations,
# e.g. +Alchemy::Picture+ or +Alchemy::Attachment+
# not on top to bottom ones like +Alchemy::Page+.
#
module TouchElements
def self.included(base)
base.after_save(:touch_elements)
end

private

def touch_elements
return unless respond_to?(:elements)

elements.map(&:touch)
end
end
end
2 changes: 0 additions & 2 deletions app/serializers/alchemy/content_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class ContentSerializer < ActiveModel::Serializer
:name,
:ingredient,
:element_id,
:created_at,
:updated_at,
:settings

has_one :essence, polymorphic: true
Expand Down
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_boolean_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Alchemy
class EssenceBooleanSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:value,
:created_at,
:updated_at
)
end
end
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_date_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Alchemy
class EssenceDateSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:date,
:created_at,
:updated_at
)
end
end
6 changes: 4 additions & 2 deletions app/serializers/alchemy/essence_file_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

module Alchemy
class EssenceFileSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:title,
:css_class
:css_class,
)

has_one :attachment
end
Expand Down
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_html_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Alchemy
class EssenceHtmlSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:source,
:created_at,
:updated_at
)
end
end
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_link_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module Alchemy
class EssenceLinkSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:link,
:link_title,
:link_target,
:link_class_name,
:created_at,
:updated_at
)
end
end
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_picture_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module Alchemy
class EssencePictureSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:picture_id,
:caption,
:title,
:alt_tag,
:css_class,
:link,
:created_at,
:updated_at
)

has_one :picture

Expand Down
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_richtext_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Alchemy
class EssenceRichtextSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:body,
:stripped_body,
:created_at,
:updated_at
)
end
end
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_select_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Alchemy
class EssenceSelectSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:value,
:created_at,
:updated_at
)
end
end
6 changes: 3 additions & 3 deletions app/serializers/alchemy/essence_text_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module Alchemy
class EssenceTextSerializer < ActiveModel::Serializer
attributes :id,
attributes(
:id,
:body,
:link,
:created_at,
:updated_at
)

def link
return if object.link.blank?
Expand Down
22 changes: 0 additions & 22 deletions db/migrate/20200226213334_alchemy_four_point_four.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def up
t.string "name"
t.references "essence", null: false, polymorphic: true, index: { unique: true }
t.references "element", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

Expand Down Expand Up @@ -57,17 +55,13 @@ def up
unless table_exists?("alchemy_essence_booleans")
create_table "alchemy_essence_booleans", force: :cascade do |t|
t.boolean "value"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["value"], name: "index_alchemy_essence_booleans_on_value"
end
end

unless table_exists?("alchemy_essence_dates")
create_table "alchemy_essence_dates", force: :cascade do |t|
t.datetime "date"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

Expand All @@ -76,17 +70,13 @@ def up
t.references "attachment"
t.string "title"
t.string "css_class"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "link_text"
end
end

unless table_exists?("alchemy_essence_htmls")
create_table "alchemy_essence_htmls", force: :cascade do |t|
t.text "source"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

Expand All @@ -96,16 +86,12 @@ def up
t.string "link_title"
t.string "link_target"
t.string "link_class_name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

unless table_exists?("alchemy_essence_pages")
create_table "alchemy_essence_pages", force: :cascade do |t|
t.references "page"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

Expand All @@ -120,8 +106,6 @@ def up
t.string "link_title"
t.string "css_class"
t.string "link_target"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "crop_from"
t.string "crop_size"
t.string "render_size"
Expand All @@ -133,16 +117,12 @@ def up
t.text "body"
t.text "stripped_body"
t.boolean "public"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

unless table_exists?("alchemy_essence_selects")
create_table "alchemy_essence_selects", force: :cascade do |t|
t.string "value"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["value"], name: "index_alchemy_essence_selects_on_value"
end
end
Expand All @@ -155,8 +135,6 @@ def up
t.string "link_class_name"
t.boolean "public", default: false
t.string "link_target"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

Expand Down
10 changes: 4 additions & 6 deletions lib/alchemy/essence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def acts_as_essence(options = {})
delegate :trashed?, to: :element, allow_nil: true
delegate :public?, to: :element, allow_nil: true
after_update :touch_content
after_save :touch_element
def acts_as_essence_class
#{name}
Expand Down Expand Up @@ -236,11 +236,9 @@ def definition
element.content_definitions.detect { |c| c["name"] == content.name } || {}
end

# Touch content. Called after update.
def touch_content
return nil if content.nil?

content.touch
# Touches element. Called after save.
def touch_element
element&.touch
end

# Returns the first x (default 30) characters of ingredient for the Element#preview_text method.
Expand Down
Loading

0 comments on commit 34b1721

Please sign in to comment.