Sha256: 2a07b2e9848cb37eaf5e19a8edb34dfd2fd56f023c780565993aa7d9bf1ce5dd

Contents?: true

Size: 1.54 KB

Versions: 32

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  # This model holds all the data needed to send a newsletter.
  class Newsletter < ApplicationRecord
    include Decidim::Traceable
    include Decidim::Loggable
    include Decidim::TranslatableResource

    translatable_fields :subject

    belongs_to :author, class_name: "User"
    belongs_to :organization

    validates :subject, presence: true
    validate :author_belongs_to_organization

    def self.log_presenter_class_for(_log)
      Decidim::AdminLog::NewsletterPresenter
    end

    # Returns true if this newsletter was already sent.
    #
    # Returns a Boolean.
    def sent?
      sent_at.present?
    end

    def sent_scopes_ids
      extended_data["scope_ids"] || []
    end

    def sent_scopes
      @sent_scopes ||= organization.scopes.where(id: sent_scopes_ids)
    end

    def sended_to_all_users?
      extended_data["send_to_all_users"]
    end

    def sended_to_followers?
      extended_data["send_to_followers"]
    end

    def sended_to_participants?
      extended_data["send_to_participants"]
    end

    def sended_to_partipatory_spaces
      extended_data["participatory_space_types"]
    end

    def template
      @template ||= Decidim::ContentBlock
                    .for_scope(:newsletter_template, organization: organization)
                    .find_by(scoped_resource_id: id)
    end

    private

    def author_belongs_to_organization
      return if !author || !organization

      errors.add(:author, :invalid) unless author.organization == organization
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
decidim-core-0.27.1 app/models/decidim/newsletter.rb
decidim-core-0.26.4 app/models/decidim/newsletter.rb
decidim-core-0.27.0 app/models/decidim/newsletter.rb
decidim-core-0.26.3 app/models/decidim/newsletter.rb
decidim-core-0.27.0.rc2 app/models/decidim/newsletter.rb
decidim-core-0.27.0.rc1 app/models/decidim/newsletter.rb
decidim-core-0.26.2 app/models/decidim/newsletter.rb
decidim-core-0.26.1 app/models/decidim/newsletter.rb
decidim-core-0.26.0 app/models/decidim/newsletter.rb
decidim-core-0.26.0.rc2 app/models/decidim/newsletter.rb
decidim-core-0.26.0.rc1 app/models/decidim/newsletter.rb
decidim-core-0.25.2 app/models/decidim/newsletter.rb
decidim-core-0.25.1 app/models/decidim/newsletter.rb
decidim-core-0.25.0 app/models/decidim/newsletter.rb
decidim-core-0.25.0.rc4 app/models/decidim/newsletter.rb
decidim-core-0.25.0.rc3 app/models/decidim/newsletter.rb
decidim-core-0.25.0.rc2 app/models/decidim/newsletter.rb
decidim-core-0.25.0.rc1 app/models/decidim/newsletter.rb
decidim-core-0.24.3 app/models/decidim/newsletter.rb
decidim-core-0.23.6 app/models/decidim/newsletter.rb