-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathresource.rb
More file actions
139 lines (117 loc) · 5.06 KB
/
Copy pathresource.rb
File metadata and controls
139 lines (117 loc) · 5.06 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Resource < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include Rails.application.routes.url_helpers
PUBLISHED_KINDS = ["Handout", "Scholarship", "Template", "Toolkit", "Form"]
KINDS = PUBLISHED_KINDS + ["Resource", "Story"]
has_rich_text :rhino_text
belongs_to :user
belongs_to :workshop, optional: true
belongs_to :windows_type, optional: true
has_one :form, as: :owner
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
has_many :categorizable_items, dependent: :destroy, as: :categorizable
has_many :reports, as: :owner
has_many :sectorable_items, dependent: :destroy, as: :sectorable
has_many :workshop_resources, dependent: :destroy
# has_many through
has_many :categories, through: :categorizable_items
has_many :related_workshops, through: :sectors, source: :workshops
has_many :sectors, through: :sectorable_items, source: :sector
# Image associations
has_many :attachments, as: :owner, dependent: :destroy # TODO - convert to GalleryImages
has_many :images, as: :owner, dependent: :destroy # TODO - convert to GalleryImages
has_one :main_image, -> { where(type: "Images::MainImage") },
as: :owner, class_name: "Images::MainImage", dependent: :destroy
has_many :gallery_images, -> { where(type: "Images::GalleryImage") },
as: :owner, class_name: "Images::GalleryImage", dependent: :destroy
# Default values
attribute :inactive, :boolean, default: false
# Validations
validates :title, presence: true, uniqueness: { case_sensitive: false }
validates :kind, presence: true
# Nested attributes
accepts_nested_attributes_for :main_image, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :gallery_images, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :form, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :categorizable_items,
allow_destroy: true,
reject_if: proc { |resource| Resource.reject?(resource) }
accepts_nested_attributes_for :sectorable_items,
allow_destroy: true,
reject_if: proc { |resource| Resource.reject?(resource) }
# Search Cop
include SearchCop
search_scope :search do
attributes :title, :author, :text
end
# Scopes
scope :by_created, -> { order(created_at: :desc) }
scope :category_names, ->(names) { tag_names(:categories, names) }
scope :sector_names, ->(names) { tag_names(:sectors, names) }
scope :featured, -> (featured=nil) { featured.present? ? where(featured: featured) : where(featured: true) }
scope :kind, -> (kind) { where("kind like ?", kind ) }
scope :leader_spotlights, -> { kind("LeaderSpotlight") }
scope :published_kinds, -> { where(kind: PUBLISHED_KINDS) }
scope :published, ->(published=nil) {
if ["true", "false"].include?(published)
result = where(inactive: published == "true" ? false : true)
else
result = where(inactive: false)
end
result.published_kinds
}
scope :published_search, ->(published_search=nil) { published_search.present? ? published(published_search) : published_kinds }
scope :recent, -> { published.by_created }
scope :sector_impact, -> { where(kind: "SectorImpact") }
scope :scholarship, -> { where(kind: "Scholarship") }
scope :story, -> { where(kind: ["Story", "LeaderSpotlight"]).order(created_at: :desc) }
scope :theme, -> { where(kind: "Theme") }
scope :title, -> (title) { where("title like ?", "%#{ title }%") }
def self.search_by_params(params)
resources = all
resources = resources.search(params[:query]) if params[:query].present? # SearchCop incl title, author, text
resources = resources.sector_names(params[:sector_names]) if params[:sector_names].present?
resources = resources.category_names(params[:category_names]) if params[:category_names].present?
resources = resources.windows_type_name(params[:windows_type_name]) if params[:windows_type_name].present?
resources = resources.title(params[:title]) if params[:title].present?
resources = resources.kind(params[:kind]) if params[:kind].present?
resources = resources.published_search(params[:published_search]) if params[:published_search].present?
resources = resources.featured(params[:featured]) if params[:featured].present?
resources
end
def story?
["Story", "LeaderSpotlight"].include? self.kind
end
def custom_label_list
"#{self.title} (#{self.kind.upcase})" unless self.kind.nil?
end
# Methods
def led_count
0
end
def name
title || id
end
def download_attachment
main_image || gallery_images.first || attachments.first
end
def type_enum
types.map { |title| [title.titleize, title ]}
end
def types
['Resource', 'LeaderSpotlight', 'SectorImpact',
'Story', 'Theme', 'Scholarship', 'TemplateAndHandout',
'ToolkitAndForm'
]
end
def year
created_at.year
end
def month
created_at.month
end
private
def self.reject?(resource)
resource['_create'] == '0'
end
end