Created
November 1, 2018 23:15
-
-
Save drusepth/7e3293236bb8b0e92b72bd60b2862259 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def content( | |
content_types: Rails.application.config.content_types[:all].map(&:name), | |
page_scoping: { user_id: self.id } | |
) | |
# @user_content ||= content_list(page_scoping: page_scoping, content_types: content_types).group_by(&:page_type) | |
polymorphic_content_fields = [:id, :name, :page_type, :user_id, :created_at, :updated_at, :deleted_at, :privacy] | |
where_conditions = page_scoping.map { |key, value| "#{key} = #{value}" }.join(' AND ') + ' AND deleted_at IS NULL' | |
sql = content_types.map do |page_type| | |
"SELECT #{polymorphic_content_fields.join(',')} FROM #{page_type.downcase.pluralize} WHERE #{where_conditions}" | |
end.join(' UNION ALL ') + ' ORDER BY page_type, id' | |
res = ActiveRecord::Base.connection.execute(sql) | |
@content_by_page_type ||= res.to_a.each_with_object({}) do |object, hash| | |
object.keys.each do |key| | |
object.except!(key) if key.is_a?(Integer) | |
end | |
hash[object['page_type']] ||= [] | |
hash[object['page_type']] << ContentPage.new(object) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment