app/models/decidim/proposals/proposal.rb in decidim-proposals-0.22.0 vs app/models/decidim/proposals/proposal.rb in decidim-proposals-0.23.0

- old
+ new

@@ -5,11 +5,11 @@ # The data store for a Proposal in the Decidim::Proposals component. class Proposal < Proposals::ApplicationRecord include Decidim::Resourceable include Decidim::Coauthorable include Decidim::HasComponent - include Decidim::ScopableComponent + include Decidim::ScopableResource include Decidim::HasReference include Decidim::HasCategory include Decidim::Reportable include Decidim::HasAttachments include Decidim::Followable @@ -17,18 +17,21 @@ include Decidim::Searchable include Decidim::Traceable include Decidim::Loggable include Decidim::Fingerprintable include Decidim::DataPortability - include Decidim::Hashtaggable include Decidim::Proposals::ParticipatoryTextSection include Decidim::Amendable include Decidim::NewsletterParticipant include Decidim::Randomable include Decidim::Endorsable include Decidim::Proposals::Valuatable + include Decidim::TranslatableResource + include Decidim::TranslatableAttributes + translatable_fields :title, :body + POSSIBLE_STATES = %w(not_answered evaluating accepted rejected withdrawn).freeze fingerprint fields: [:title, :body] amendable( @@ -47,11 +50,11 @@ has_many :notes, foreign_key: "decidim_proposal_id", class_name: "ProposalNote", dependent: :destroy, counter_cache: "proposal_notes_count" validates :title, :body, presence: true - geocoded_by :address, http_headers: ->(proposal) { { "Referer" => proposal.component.organization.host } } + geocoded_by :address scope :answered, -> { where.not(answered_at: nil) } scope :not_answered, -> { where(answered_at: nil) } scope :state_not_published, -> { where(state_published_at: nil) } @@ -64,31 +67,10 @@ scope :except_rejected, -> { where.not(state: "rejected").or(state_not_published) } scope :except_withdrawn, -> { where.not(state: "withdrawn").or(where(state: nil)) } scope :drafts, -> { where(published_at: nil) } scope :except_drafts, -> { where.not(published_at: nil) } scope :published, -> { where.not(published_at: nil) } - scope :official_origin, lambda { - where.not(coauthorships_count: 0) - .joins(:coauthorships) - .where(decidim_coauthorships: { decidim_author_type: "Decidim::Organization" }) - } - scope :citizens_origin, lambda { - where.not(coauthorships_count: 0) - .joins(:coauthorships) - .where.not(decidim_coauthorships: { decidim_author_type: "Decidim::Organization" }) - } - scope :user_group_origin, lambda { - where.not(coauthorships_count: 0) - .joins(:coauthorships) - .where(decidim_coauthorships: { decidim_author_type: "Decidim::UserBaseEntity" }) - .where.not(decidim_coauthorships: { decidim_user_group_id: nil }) - } - scope :meeting_origin, lambda { - where.not(coauthorships_count: 0) - .joins(:coauthorships) - .where(decidim_coauthorships: { decidim_author_type: "Decidim::Meetings::Meeting" }) - } scope :sort_by_valuation_assignments_count_asc, lambda { order(sort_by_valuation_assignments_count_nulls_last_query + "ASC NULLS FIRST") } scope :sort_by_valuation_assignments_count_desc, lambda { @@ -105,12 +87,12 @@ acts_as_list scope: :decidim_component_id searchable_fields({ scope_id: :decidim_scope_id, participatory_space: { component: :participatory_space }, - D: :search_body, - A: :search_title, + D: :body, + A: :title, datetime: :published_at }, index_on_create: ->(proposal) { proposal.official? }, index_on_update: ->(proposal) { proposal.visible? }) @@ -144,11 +126,11 @@ participants_has_voted_ids = Decidim::Proposals::ProposalVote.joins(:proposal).where(proposal: proposals).joins(:author).map(&:decidim_author_id).flatten.compact.uniq endorsements_participants_ids = Decidim::Endorsement.where(resource: proposals) .where(decidim_author_type: "Decidim::UserBaseEntity") - .map(&:decidim_author_id).flatten.compact.uniq + .pluck(:decidim_author_id).to_a.compact.uniq (endorsements_participants_ids + participants_has_voted_ids + coauthors_recipients_ids).flatten.compact.uniq end # Public: Updates the vote count of this proposal. @@ -298,23 +280,10 @@ # Public: Whether the proposal is a draft or not. def draft? published_at.nil? end - # method for sort_link by number of comments - ransacker :commentable_comments_count do - query = <<-SQL - (SELECT COUNT(decidim_comments_comments.id) - FROM decidim_comments_comments - WHERE decidim_comments_comments.decidim_commentable_id = decidim_proposals_proposals.id - AND decidim_comments_comments.decidim_commentable_type = 'Decidim::Proposals::Proposal' - GROUP BY decidim_comments_comments.decidim_commentable_id - ) - SQL - Arel.sql(query) - end - # Defines the base query so that ransack can actually sort by this value def self.sort_by_valuation_assignments_count_nulls_last_query <<-SQL ( SELECT COUNT(decidim_proposals_valuation_assignments.id) @@ -355,9 +324,13 @@ ") end ransacker :state do Arel.sql("CASE WHEN state = 'withdrawn' THEN 'withdrawn' WHEN state_published_at IS NULL THEN NULL ELSE state END") + end + + ransacker :title do + Arel.sql(%{("decidim_proposals_proposals"."title")::text}) end ransacker :id_string do Arel.sql(%{cast("decidim_proposals_proposals"."id" as text)}) end