feature/share-libs
rb 275 lines 7.18 KB
Raw
1 class ChordsController < ApplicationController
2 def index
3 meta = Meta
4 .select(:title, :description)
5 .where(home: 'guitar_chords', locale: [I18n.locale, I18n.default_locale])
6 .last
7
8 @meta_title = meta.title if meta
9 @meta_description = meta.description if meta
10
11 @page = Page
12 .select(:page_content, :title)
13 .where(page_slug: 'guitar_chords', locale: [I18n.locale, I18n.default_locale])
14 .last
15
16 @chords = Chord
17 .select(:id, :views_count, :song_provider_id, :updated_at, :title)
18 .order(updated_at: :desc)
19 .paginate(page: params[:page], per_page: 10)
20
21 @trending_chords = Chord
22 .select(:id, :views_count, :song_provider_id, :updated_at, :title)
23 .order(views_count: :desc)
24 .limit(10)
25 end
26
27 def guitar_shapes
28 @meta_title = 'Koleksi chord gitar lagu artis-artis terbaru, artis pop, informasi artis terkini | Lyrics88.Com'
29 @meta_description = 'Koleksi chord gitar lagu artis-artis terbaru, artis pop, informasi artis terkini | Lyrics88.Com'
30
31 @page = Page
32 .select(:page_content, :title)
33 .find_by page_slug: 'guitar_chords_shapes'
34
35 @chords = ChordShape
36 .select(:id, :name)
37 .all
38 end
39
40 def guitar_shape
41 @meta_title = 'Koleksi chord gitar lagu artis-artis terbaru, artis pop, informasi artis terkini | Lyrics88.Com'
42 @meta_description = 'Koleksi chord gitar lagu artis-artis terbaru, artis pop, informasi artis terkini | Lyrics88.Com'
43
44 @chords = ChordShape
45 .select(:id, :name)
46 .all
47 id = params[:slug].split('-')[-1]
48 @chord = ChordShape.find_by id: id
49 end
50
51 def chord
52 id = params[:slug].split('-')[-1]
53 @chord = Chord.find_by(id: id)
54
55 if @chord.nil?
56 render_chord_nil
57 meta
58 else
59 render_chord
60 meta
61 end
62 end
63
64 def history
65 id = params[:slug].split('-')[-1]
66 @chord = Chord.find_by(id: id)
67
68 return redirect_to guitar_chords_path if @chord.nil?
69
70 @chord_histories = ChordHistory.where(chord_id: id).order(updated_at: :desc)
71
72 @song = @chord.song_provider
73 @page_title = if @song.nil?
74 @chord.title
75 else
76 t(
77 'song_chord_page_title',
78 song_name: @song.name
79 )
80 end
81
82 @chords = Chord.where(song_id: @song.id) unless @song.nil?
83 @chord.increment!(:views_count) unless browser.bot?
84
85 @author = @chord.user.nil? ? 'nowhereman' : @chord.user.username_or_id
86
87 @meta_title = @page_title
88 @meta_description = @page_title
89 end
90
91 def add
92 @chord = Chord.new
93 @user = current_user
94 @page_title = t('add_chord_title')
95 @meta_title = @page_title
96 @meta_description = @page_title
97 end
98
99 def add_post
100 @chord = Chord.new(chord_params)
101
102 re_render_add && return unless @chord.save
103
104 add_chord_history
105 flash[:success] = t('add_chord_validation_success')
106 redirect_to guitar_chord_detail_path(@chord.slug)
107 end
108
109 def re_render_add
110 @user = current_user
111 @page_title = t('add_chord_title')
112 @meta_title = @page_title
113 @meta_description = @page_title
114 @chord_artists = chord_artists_link
115
116 flash[:warning] = t('add_chord_validation')
117 render :add
118 end
119
120 def chord_artists_link
121 @chord.song
122 &.artists
123 &.map { |a| view_context.link_to(a.name, artist_detail_path(a.slug)) }
124 &.join(' ')
125 &.html_safe
126 end
127
128 def song_search
129 render json: Song
130 .where('lower(name) LIKE ?', "%#{params[:s].downcase}%")
131 .as_json(only: %i[id name])
132 end
133
134 def review
135 id = params[:slug].split('-')[-1]
136 @chord = Chord.find_by(id: id)
137
138 redirect_to guitar_chords_path and return if @chord.nil?
139
140 unless @chord.pending?
141 flash[:warning] = 'Already approved.'
142 redirect_to guitar_chord_detail_path and return
143 end
144
145 if request.patch?
146 unless user_signed_in?
147 flash[:warning] = 'Please login to review.'
148 redirect_to guitar_chord_detail_path and return
149 end
150
151 unless current_user.reviewer?
152 flash[:warning] = 'You have to have 10 €BACH to review.'
153 redirect_to guitar_chord_review_path
154 return
155 end
156
157 @chord.update_attribute(:status, :approved)
158
159 chord_history = ChordHistory.new(
160 chord_id: @chord.id,
161 user_id: current_user.id,
162 name: 'Approved'
163 )
164 unless chord_history.save
165 # Revert
166 @chord.update_attribute(:status, :pending)
167 flash[:warning] = 'Unknown error'
168 redirect_to guitar_chord_review_path
169 return
170 end
171
172 flash[:success] = 'Approved!'
173 redirect_to guitar_chord_detail_path(@chord.slug)
174 return
175 end
176
177 @song = @chord.song_provider
178 @page_title = if @song.nil?
179 @chord.title
180 else
181 t(
182 'song_chord_page_title',
183 song_name: @song.name
184 )
185 end
186
187 @chords = Chord.where(song_id: @song.id) unless @song.nil?
188 @chord.increment!(:views_count) unless browser.bot?
189
190 @author = @chord.user.nil? ? 'nowhereman' : @chord.user.username_or_id
191
192 @meta_title = @page_title
193 @meta_description = @page_title
194 end
195
196 def vote
197 redirect_to_login && return unless user_signed_in?
198
199 vote = ChordVote.new(chord_id: params[:id], user_id: current_user.id)
200 vote.save
201
202 redirect_back(fallback_location: root_path)
203 end
204
205 def unvote
206 redirect_to_login && return unless user_signed_in?
207
208 vote = ChordVote.where(chord_id: params[:id], user_id: current_user.id)
209 vote.destroy_all
210
211 redirect_back(fallback_location: root_path)
212 end
213
214 private
215
216 def chord_params
217 params
218 .require(:chord)
219 .permit(:title, :chord, :user_id, :song_id)
220 end
221
222 def redirect_to_login
223 flash[:success] = t('need_to_logged_in')
224 chord = Chord.find_by(id: params[:id])
225 redirect_to login_path(redirect_to: guitar_chord_detail_path(chord.slug))
226 end
227
228 def add_chord_history
229 chord_history = ChordHistory.new(
230 chord_id: @chord.id,
231 user_id: @chord.user_or_nowhereman.id,
232 name: 'Submitted'
233 )
234 chord_history.save
235 end
236
237 def render_chord
238 @song = @chord.song_provider
239 @page_title = chord_page_title
240 @chord_content = chord_content
241 @chords = Chord.where(song_id: @song.id) unless @song.nil?
242 @chord.increment!(:views_count) unless browser.bot?
243 @author = @chord.user.nil? ? 'nowhereman' : @chord.user.username_or_id
244 end
245
246 def render_chord_nil
247 @page_title = 'Chord is not found'
248 end
249
250 def chord_content
251 if user_signed_in?
252 @chord.chord
253 else
254 song_name = @song&.name || @chord.title
255 t('join_our_chord_community', song_name: song_name)
256 end
257 end
258
259 def chord_page_title
260 if @song.nil?
261 @chord.title
262 else
263 t(
264 'song_chord_page_title',
265 song_name: @song.name
266 # Should call Spotify API here to get the artist name
267 )
268 end
269 end
270
271 def meta
272 @meta_title = @page_title
273 @meta_description = @page_title
274 end
275 end