Sha256: e6e04e2e8abb4ab77998f008a36328c1597e82d8c9b5346cfc665e5d4eb8943f

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

module LatoBlog
  class PostField < ApplicationRecord

    include PostField::EntityHelpers
    include PostField::SerializerHelpers

    # Properties:

    serialize :meta_datas, Hash

    # Relations:

    belongs_to :post, foreign_key: :lato_blog_post_id,
                      class_name: 'LatoBlog::Post'

    has_many :post_fields, foreign_key: :lato_blog_post_field_id,
                           class_name: 'LatoBlog::PostField',
                           dependent: :destroy
    belongs_to :post_field, foreign_key: :lato_blog_post_field_id,
                            class_name: 'LatoBlog::PostField',
                            optional: true

    # Validations:

    validates :key, presence: true

    # Scopes:

    scope :visibles, -> { where(meta_visible: true) }
    scope :not_visibles, -> { where(meta_visible: false) }
    scope :roots, -> { where(lato_blog_post_field_id: nil) }

    # Callbacks:

    after_update do
      update_child_visibility
    end

    private

    # This functions update all post fields child visibility with the current
    # post field visibility.
    def update_child_visibility
      return if meta_visible == meta_visible_was
      post_fields.map { |pf| pf.update(meta_visible: meta_visible) }
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lato_blog-2.2.2 app/models/lato_blog/post_field.rb
lato_blog-2.2.1 app/models/lato_blog/post_field.rb
lato_blog-2.2.0 app/models/lato_blog/post_field.rb
lato_blog-2.1.2 app/models/lato_blog/post_field.rb
lato_blog-2.1.1 app/models/lato_blog/post_field.rb
lato_blog-2.1 app/models/lato_blog/post_field.rb