app/queries/decidim/initiatives/admin/manageable_initiatives.rb in decidim-initiatives-0.20.1 vs app/queries/decidim/initiatives/admin/manageable_initiatives.rb in decidim-initiatives-0.21.0

- old
+ new

@@ -5,60 +5,28 @@ module Admin # Class that retrieves manageable initiatives for the given user. # Regular users will get only their initiatives. Administrators will # retrieve all initiatives. class ManageableInitiatives < Rectify::Query - attr_reader :organization, :user, :q, :state - # Syntactic sugar to initialize the class and return the queried objects # - # organization - Decidim::Organization - # user - Decidim::User - # query - String - # state - String - def self.for(organization, user, query, state) - new(organization, user, query, state).query + # user - Decidim::User + def self.for(user) + new(user).query end # Initializes the class. # - # organization - Decidim::Organization - # user - Decidim::User - # query - String - # state - String - def initialize(organization, user, query, state) - @organization = organization + # user - Decidim::User + def initialize(user) @user = user - @q = query - @state = state end # Retrieves all initiatives / Initiatives created by the user. def query - if user.admin? - base = Initiative - .where(organization: organization) - .with_state(state) - else - ids = InitiativesCreated.by(user).with_state(state).pluck(:id) - ids += InitiativesPromoted.by(user).with_state(state).pluck(:id) - base = Initiative.where(id: ids) - end + return Initiative.where(organization: @user.organization) if @user.admin? - return base if q.blank? - - organization.available_locales.each_with_index do |loc, index| - base = if index.zero? - base.where("title->>? ilike ?", loc, "%#{q}%") - .or(Initiative.where("description->>? ilike ?", loc, "%#{q}%")) - else - base - .or(Initiative.where("title->>? ilike ?", loc, "%#{q}%")) - .or(Initiative.where("description->>? ilike ?", loc, "%#{q}%")) - end - end - - base + Initiative.where(id: InitiativesCreated.by(@user) + InitiativesPromoted.by(@user)) end end end end end