app/models/decidim/proposals/proposal.rb in decidim-proposals-0.8.4 vs app/models/decidim/proposals/proposal.rb in decidim-proposals-0.9.0

- old
+ new

@@ -16,35 +16,29 @@ include Decidim::Comments::Commentable feature_manifest_name "proposals" has_many :votes, foreign_key: "decidim_proposal_id", class_name: "ProposalVote", dependent: :destroy, counter_cache: "proposal_votes_count" + 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.feature.organization.host } } scope :accepted, -> { where(state: "accepted") } scope :rejected, -> { where(state: "rejected") } scope :evaluating, -> { where(state: "evaluating") } + scope :withdrawn, -> { where(state: "withdrawn") } + scope :except_withdrawn, -> { where.not(state: "withdrawn").or(where(state: nil)) } def self.order_randomly(seed) transaction do connection.execute("SELECT setseed(#{connection.quote(seed)})") order("RANDOM()").load end end - def author_name - return I18n.t("decidim.proposals.models.proposal.fields.official_proposal") if official? - user_group&.name || author.name - end - - def author_avatar_url - author&.avatar&.url || ActionController::Base.helpers.asset_path("decidim/default-avatar.svg") - end - # Public: Check if the user has voted the proposal. # # Returns Boolean. def voted_by?(user) votes.where(author: user).any? @@ -76,10 +70,17 @@ # Returns Boolean. def evaluating? answered? && state == "evaluating" end + # Public: Checks if the author has withdrawn the proposal. + # + # Returns Boolean. + def withdrawn? + state == "withdrawn" + end + # Public: Overrides the `commentable?` Commentable concern method. def commentable? feature.settings.comments_enabled? end @@ -144,9 +145,29 @@ # Checks whether the user can edit the given proposal. # # user - the user to check for authorship def editable_by?(user) authored_by?(user) && !answered? && within_edit_time_limit? + end + + # Checks whether the user can withdraw the given proposal. + # + # user - the user to check for withdrawability. + def withdrawable_by?(user) + user && !withdrawn? && authored_by?(user) + 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 private # Checks whether the proposal is inside the time window to be editable or not.