Move lib folder inside the app folder

The purpose of the lib folder is to have code that doesn't necessary
belong in the application but can be shared with other applications.

However, we don't have other applications and, if we did, the way to
share code between them would be using a gem or even a git submodule.

So having both the `app/` and the `lib/` folders is confusing IMHO, and
it causes unnecessary problems with autoloading.

So we're moving the `lib/` folder to `app/lib/`. Originally, some of
these files were in the `app/services/` folder and then they were moved
to the `lib/` folder. We're using `app/lib/` instead of `app/services/`
so the upgrade is less confusing.

There's an exception, though. The `OmniAuth::Strategies::Wordpress`
class needs to be available in the Devise initializer. Since this is an
initializer and trying to autoload a class here will be problematic when
switching to Zeitwerk, we'll keep the `require` clause on top of the
Devise initializer in order to load the file and so it will be loaded
even if it isn't in the autoload paths anymore.
This commit is contained in:
Javi Martín
2024-03-17 21:35:17 +01:00
parent 913b93aea7
commit cb477149c4
40 changed files with 2 additions and 9 deletions

95
app/lib/user_segments.rb Normal file
View File

@@ -0,0 +1,95 @@
class UserSegments
def self.segments
%w[all_users
administrators
all_proposal_authors
proposal_authors
investment_authors
feasible_and_undecided_investment_authors
selected_investment_authors
winner_investment_authors
not_supported_on_current_budget] + geozones.keys
end
def self.segment_name(segment)
geozones[segment.to_s]&.name || I18n.t("admin.segment_recipient.#{segment}") if valid_segment?(segment)
end
def self.all_users
User.active.where.not(confirmed_at: nil)
end
def self.administrators
all_users.administrators
end
def self.all_proposal_authors
author_ids(Proposal.pluck(:author_id))
end
def self.proposal_authors
author_ids(Proposal.not_archived.not_retired.pluck(:author_id))
end
def self.investment_authors
author_ids(current_budget_investments.pluck(:author_id))
end
def self.feasible_and_undecided_investment_authors
unfeasible_and_finished_condition = "feasibility = 'unfeasible' and valuation_finished = true"
investments = current_budget_investments.where.not(unfeasible_and_finished_condition)
author_ids(investments.pluck(:author_id))
end
def self.selected_investment_authors
author_ids(current_budget_investments.selected.pluck(:author_id))
end
def self.winner_investment_authors
author_ids(current_budget_investments.winners.pluck(:author_id))
end
def self.not_supported_on_current_budget
author_ids(
User.where.not(
id: Vote.select(:voter_id).where(votable: current_budget_investments).distinct
)
)
end
def self.valid_segment?(segment)
segments.include?(segment.to_s)
end
def self.recipients(segment)
if geozones[segment.to_s]
all_users.where(geozone: geozones[segment.to_s])
else
send(segment)
end
end
def self.user_segment_emails(segment)
recipients(segment).newsletter.order(:created_at).pluck(:email).compact
end
private
def self.current_budget_investments
Budget.current.investments
end
def self.author_ids(author_ids)
all_users.where(id: author_ids)
end
def self.geozones
Geozone.order(:name).to_h do |geozone|
[geozone.name.gsub(/./) { |char| character_approximation(char) }.underscore.tr(" ", "_"), geozone]
end
end
def self.character_approximation(char)
I18n::Backend::Transliterator::HashTransliterator::DEFAULT_APPROXIMATIONS[char] || char
end
end