Class: Decidim::Initiatives::Admin::ManageableInitiatives
- Inherits:
-
Rectify::Query
- Object
- Rectify::Query
- Decidim::Initiatives::Admin::ManageableInitiatives
- 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
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.for(organization, user, query, state) ⇒ Object
Syntactic sugar to initialize the class and return the queried objects.
Instance Method Summary collapse
-
#initialize(organization, user, query, state) ⇒ ManageableInitiatives
constructor
Initializes the class.
-
#query ⇒ Object
Retrieves all initiatives / Initiatives created by the user.
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
#organization ⇒ Object (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 |
#q ⇒ Object (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 |
#state ⇒ Object (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 |
#user ⇒ Object (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
#query ⇒ Object
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 |