Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Open source Post and Post Category models (#12321)

Łukasz Adamczyk2a78d4c4c538
diff --git a/core/app/models/spree/post.rb b/core/app/models/spree/post.rb
new file mode 100644
index 0000000000..1e172a62e2
--- /dev/null
+++ b/core/app/models/spree/post.rb
@@ -0,0 +1,108 @@
+module Spree
+ class Post < Spree.base_class
+ include Spree::SingleStoreResource
+ extend FriendlyId
+
+ friendly_id :slug_candidates, use: %i[slugged scoped history], scope: %i[store_id deleted?]
+ acts_as_paranoid
+ acts_as_taggable_on :tags
+ acts_as_taggable_tenant :store_id
+
+ if defined?(PgSearch)
+ include PgSearch::Model
+ pg_search_scope :search_by_title, against: :title
+ else
+ scope :search_by_title, ->(query) { where('title LIKE ?', "%#{query}%") }
+ end
+
+ #
+ # Ransack filtering
+ #
+ self.whitelisted_ransackable_attributes = %w[author_id post_category_id published_at]
+ self.whitelisted_ransackable_associations = %w[author post_category]
+ self.whitelisted_ransackable_scopes = %w[search_by_title]
+
+ #
+ # Attachments
+ #
+ has_one_attached :image, service: Spree.public_storage_service_name
+
+ #
+ # Rich Text
+ #
+ has_rich_text :content
+ has_rich_text :excerpt
+
+ #
+ # Associations
+ #
+ belongs_to :author, class_name: Spree.admin_user_class.to_s
+ belongs_to :store, class_name: 'Spree::Store'
+ belongs_to :post_category, class_name: 'Spree::PostCategory', optional: true
+ alias category post_category
+
+ #
+ # Validations
+ #
+ validates :title, :store, :author, presence: true
+ validates :slug, presence: true, uniqueness: { scope: :store_id }
+ validates :meta_title, length: { maximum: 160 }, allow_blank: true
+ validates :meta_description, length: { maximum: 320 }, allow_blank: true
+ validates :image, content_type: Rails.application.config.active_storage.web_image_content_types
+
+ #
+ # Scopes
+ #
+ scope :published, -> { where(published_at: [..Time.current]) }
+
+ delegate :name, to: :author, prefix: true, allow_nil: true
+ delegate :title, to: :post_category, prefix: true, allow_nil: true
+
+ def should_generate_new_friendly_id?
+ slug.blank? || (persisted? && title_changed?)
+ end
+
+ def slug_candidates
+ [
+ :title,
+ [:title, :id]
+ ]
+ end
+
+ def published?
+ published_at.present?
+ end
+
+ def url
+ return unless Spree::Core::Engine.routes.url_helpers.respond_to?(:post_path)
+
+ Spree::Core::Engine.routes.url_helpers.post_path(self, locale: I18n.locale)
+ end
+
+ def publish(date = nil)
+ update(published_at: date || Time.current)
+ end
+
+ def unpublish
+ update(published_at: nil)
+ end
+
+ def description
+ excerpt.to_plain_text.presence || content.to_plain_text
+ end
+
+ def shortened_description
+ desc = excerpt.to_plain_text.presence || content.to_plain_text
+ desc.length > 320 ? "#{desc[0...320]}..." : desc
+ end
+
+ def self.to_tom_select_json
+ pluck(:id, :title).map do |id, title|
+ {
+ id: id,
+ name: title
+ }
+ end.as_json
+ end
+ end
+end
diff --git a/core/app/models/spree/post_category.rb b/core/app/models/spree/post_category.rb
new file mode 100644
index 0000000000..dfd0226e34
--- /dev/null
+++ b/core/app/models/spree/post_category.rb
@@ -0,0 +1,33 @@
+module Spree
+ class PostCategory < Spree.base_class
+ include Spree::SingleStoreResource
+ extend FriendlyId
+
+ friendly_id :slug_candidates, use: %i[slugged scoped history], scope: %i[store_id]
+
+ #
+ # Associations
+ #
+ belongs_to :store, class_name: 'Spree::Store'
+ has_many :posts, class_name: 'Spree::Post'
+
+ #
+ # Validations
+ #
+ validates :title, :store, presence: true
+ validates :slug, presence: true, uniqueness: { scope: :store_id }
+
+ has_rich_text :description
+
+ def should_generate_new_friendly_id?
+ slug.blank? || title_changed?
+ end
+
+ def slug_candidates
+ [
+ :title,
+ [:title, :id]
+ ]
+ end
+ end
+end
diff --git a/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb b/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb
new file mode 100644
index 0000000000..85b0135894
--- /dev/null
+++ b/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb
@@ -0,0 +1,34 @@
+class CreateSpreePostsAndSpreePostCategories < ActiveRecord::Migration[6.1]
+ def change
+ if !table_exists?(:spree_post_categories)
+ create_table :spree_post_categories do |t|
+ t.references :store, null: false, index: true
+ t.string :title, null: false
+ t.string :slug, null: false
+
+ t.timestamps
+
+ t.index ['slug', 'store_id'], name: 'index_spree_post_categories_on_slug_and_store_id', unique: true
+ end
+ end
+
+ if !table_exists?(:spree_posts)
+ create_table :spree_posts do |t|
+ t.references :author, index: true
+ t.datetime :published_at
+ t.string :title, null: false
+ t.string :slug, null: false
+ t.references :post_category, index: true
+ t.references :store, index: true
+ t.string :meta_title
+ t.string :meta_description
+
+ t.timestamps
+ t.datetime :deleted_at
+
+ t.index ['slug', 'store_id'], name: 'index_spree_posts_on_slug_and_store_id', unique: true, where: '(deleted_at IS NULL)'
+ t.index ['title'], name: 'index_spree_posts_on_title'
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/post_category_factory.rb b/core/lib/spree/testing_support/factories/post_category_factory.rb
new file mode 100644
index 0000000000..fe0cb9a6fa
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/post_category_factory.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :post_category, class: Spree::PostCategory do
+ sequence(:title) { |n| "Category ##{n + 1}" }
+ description { FFaker::Lorem.sentence }
+ store { Spree::Store.default || create(:store) }
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/post_factory.rb b/core/lib/spree/testing_support/factories/post_factory.rb
new file mode 100644
index 0000000000..6840c5ff5d
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/post_factory.rb
@@ -0,0 +1,10 @@
+FactoryBot.define do
+ factory :post, class: Spree::Post do
+ post_category { create(:post_category) }
+ title { FFaker::Lorem.sentence }
+ content { FFaker::Lorem.paragraph }
+ published_at { Time.current }
+ author { create(:admin_user) }
+ store { Spree::Store.default || create(:store) }
+ end
+end
diff --git a/core/spec/models/spree/post_spec.rb b/core/spec/models/spree/post_spec.rb
new file mode 100644
index 0000000000..a63675da68
--- /dev/null
+++ b/core/spec/models/spree/post_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Post, type: :model do
+ let(:post) { create(:post) }
+
+ describe 'validations' do
+ describe 'image' do
+ it 'validates content type' do
+ post.image.attach(
+ io: File.open(Spree::Core::Engine.root.join('spec/fixtures/files/icon_256x256.png')), filename: 'icon_256x256.png',
+ content_type: 'image/png'
+ )
+ expect(post).to be_valid
+
+ post.image.attach(
+ io: File.open(Spree::Core::Engine.root.join('spec/fixtures/files/example.json')), filename: 'example.json',
+ content_type: 'application/json'
+ )
+ expect(post).not_to be_valid
+ end
+ end
+ end
+end