Class: Decidim::Initiatives::Admin::ManageableInitiatives

Inherits:
Rectify::Query
  • Object
show all
Defined in:
app/queries/decidim/initiatives/admin/manageable_initiatives.rb

Overview

Class that retrieves manageable initiatives for the given user. Regular users will get only their initiatives. Administrators will retrieve all initiatives.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization, user, query, state) ⇒ ManageableInitiatives

Initializes the class.

organization - Decidim::Organization user - Decidim::User query - String state - String



28
29
30
31
32
33
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 28

def initialize(organization, user, query, state)
  @organization = organization
  @user = user
  @q = query
  @state = state
end

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



10
11
12
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 10

def organization
  @organization
end

#qObject (readonly)

Returns the value of attribute q.



10
11
12
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 10

def q
  @q
end

#stateObject (readonly)

Returns the value of attribute state.



10
11
12
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 10

def state
  @state
end

#userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 10

def user
  @user
end

Class Method Details

.for(organization, user, query, state) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects

organization - Decidim::Organization user - Decidim::User query - String state - String



18
19
20
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 18

def self.for(organization, user, query, state)
  new(organization, user, query, state).query
end

Instance Method Details

#queryObject

Retrieves all initiatives / Initiatives created by the user.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/queries/decidim/initiatives/admin/manageable_initiatives.rb', line 36

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 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
end