-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtutorial.rb
More file actions
89 lines (75 loc) · 3.55 KB
/
tutorial.rb
File metadata and controls
89 lines (75 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class VideoLibrary < ApplicationRecord
self.table_name = 'tutorials'
include Featureable, Publishable, TagFilterable, Trendable, RichTextSearchable
has_rich_text :rhino_body
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
has_many :categorizable_items, dependent: :destroy, inverse_of: :categorizable, as: :categorizable
has_many :sectorable_items, dependent: :destroy, inverse_of: :sectorable, as: :sectorable
has_many :categories, through: :categorizable_items
has_many :sectors, through: :sectorable_items
# Asset associations
has_one :primary_asset, -> { where("type" => "PrimaryAsset") },
as: :owner, class_name: "PrimaryAsset", dependent: :destroy
has_many :gallery_assets, -> { where("type" => "GalleryAsset") },
as: :owner, class_name: "GalleryAsset", dependent: :destroy
has_many :assets, as: :owner, dependent: :destroy
validates :title, presence: true, uniqueness: { case_sensitive: false }
# Nested attributes
accepts_nested_attributes_for :primary_asset, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :gallery_assets, reject_if: :all_blank, allow_destroy: true
# SearchCop
include SearchCop
search_scope :search do
attributes :title, :body
scope { join_rich_texts }
attributes action_text_body: "action_text_rich_texts.plain_text_body"
options :action_text_body, type: :text, default: true, default_operator: :or
end
scope :body, ->(body) { where("body like ?", "%#{ body }%") }
scope :title, ->(title) { where("title like ?", "%#{ title }%") }
scope :tutorial_name, ->(tutorial_name) { title(tutorial_name) }
scope :tutorials, -> { where(type: 'Tutorial') }
scope :podcasts, -> { where(type: 'Podcast') }
scope :intros, -> { where(type: 'Intro') }
scope :with_sector_ids, ->(sector_hash) {
ids = sector_hash.values.reject(&:blank?).map(&:to_i)
return all if ids.empty?
joins(:sectorable_items)
.where(sectorable_items: { sectorable_type: "VideoLibrary", sector_id: ids })
.distinct
}
scope :with_category_ids, ->(category_hash) {
ids = category_hash.values.reject(&:blank?).map(&:to_i)
return all if ids.empty?
joins(:categorizable_items)
.where(categorizable_items: { categorizable_type: "VideoLibrary", category_id: ids })
.distinct
}
scope :title_or_body, ->(term) {
pattern = "%#{term}%"
left_joins(:rich_text_rhino_body)
.where("tutorials.title LIKE :q OR tutorials.body LIKE :q OR action_text_rich_texts.body LIKE :q", q: pattern)
}
def self.search_by_params(params)
resources = is_a?(ActiveRecord::Relation) ? self : all
resources = resources.title_or_body(params[:search]) if params[:search].present?
resources = resources.title(params[:title]) if params[:title].present?
resources = resources.body(params[:body]) if params[:body].present?
if visibility_params_present?(params)
resources = apply_visibility_filters(resources, params)
elsif params[:published].present?
resources = resources.published(params[:published])
end
resources = resources.with_sector_ids(params[:sectors]) if params[:sectors].present?
resources = resources.with_category_ids(params[:categories]) if params[:categories].present?
resources = resources.sector_names_all(params[:sector_names_all]) if params[:sector_names_all].present?
resources = resources.category_names_all(params[:category_names_all]) if params[:category_names_all].present?
resources
end
end
class Tutorial < VideoLibrary
end
class Podcast < VideoLibrary
end
class Intro < VideoLibrary
end