feature/share-libs
rb 176 lines 4.88 KB
Raw
1 class JavaneseGamelanController < ApplicationController
2 def index
3 meta_tags('javanese-gamelan')
4 page('javanese-gamelan')
5
6 @gendings = JavaneseGending
7 .select(:id, :name, :views_count, :updated_at, :description)
8 .order(created_at: :desc)
9 .paginate(page: params[:page], per_page: 10)
10
11 @trending_gendings = @gendings.order(views_count: :desc).limit(10)
12 end
13
14 def gending
15 meta_tags('gending')
16 page('gending')
17 @gendings = JavaneseGending
18 .select(:id, :name, :user_id, :views_count, :updated_at, :description)
19 .order(created_at: :desc)
20 .paginate(page: params[:page], per_page: 10)
21 end
22
23 def gending_detail
24 id = params[:slug].split('-')[-1]
25 @gending = JavaneseGending.find_by(id: id)
26
27 return redirect_to javanese_gending_path unless @gending
28
29 @author = @gending.user_or_nowhereman
30 @page_title = t('gending_notation_page_title', title: @gending.name)
31 @meta_title = @page_title
32 @meta_description = t('gending_notation_meta_desciption', title: @gending.name)
33
34 @gending.increment!(:views_count) unless browser.bot?
35 end
36
37 # The naming inconsistency between gamelan and gending is for the sake of SEO.
38 def gamelan_notation
39 meta_tags('javanese-gamelan-notation')
40 page('javanese-gamelan-notation')
41 @gending_notations = JavaneseGendingNotation
42 .select(:id, :user_id, :name, :rich_format, :views_count, :updated_at)
43 .order(created_at: :desc)
44 .paginate(page: params[:page], per_page: 10)
45 end
46
47 # Detail notation
48 def notation
49 render_notation
50 end
51
52 def notation_new
53 render_notation_new
54 end
55
56 def add_gending
57 @javanese_gending = JavaneseGending.new
58 meta_tags('javanese-gamelan-notation')
59 end
60
61 def add
62 if request.post?
63 @javanese_gending = JavaneseGending.new(javanese_gending_params)
64 if @javanese_gending.save
65 redirect_to javanese_gamelan_path
66 else
67 flash[:warning] = t('lyric_cannot_be_empty')
68 redirect_to add_lyric_path(@lyric.song_id)
69 end
70
71 return
72 end
73
74 @javanese_gending = JavaneseGending.new
75 meta_tags('javanese-gamelan-notation')
76 end
77
78 def gending_vote
79 return redirect_to login_path unless user_signed_in?
80
81 vote = GendingVote.new(javanese_gending_id: params[:id], user_id: current_user.id)
82 vote.save
83 redirect_back(fallback_location: root_path)
84 end
85
86 def gending_unvote
87 return redirect_to login_path unless user_signed_in?
88
89 vote = GendingVote.where(javanese_gending_id: params[:id], user_id: current_user.id)
90 vote.destroy_all
91 redirect_back(fallback_location: root_path)
92 end
93
94 def gending_notation_vote
95 return redirect_to login_path unless user_signed_in?
96
97 vote = GendingNotationVote.new(javanese_gending_notation_id: params[:id], user_id: current_user.id)
98 vote.save
99 redirect_back(fallback_location: root_path)
100 end
101
102 def gending_notation_unvote
103 return redirect_to login_path unless user_signed_in?
104
105 vote = GendingNotationVote.where(javanese_gending_notation_id: params[:id], user_id: current_user.id)
106 vote.destroy_all
107 redirect_back(fallback_location: root_path)
108 end
109
110 private
111
112 def render_notation
113 check_valid_slug
114
115 @content_notation = content_notation
116 notation_info
117 end
118
119 def render_notation_new
120 check_valid_slug
121 @content_notation = content_notation_new
122 notation_info
123 end
124
125 def check_valid_slug
126 id = params[:slug].split('-')[-1]
127 @gending = JavaneseGendingNotation.find_by(id: id)
128 return redirect_to javanese_gending_gamelan_notation_path if @gending.nil?
129 end
130
131 def content_notation
132 if user_signed_in?
133 @gending.notation
134 else
135 t('join_our_javanese_community', gending_name: @gending.name)
136 end
137 end
138
139 def content_notation_new
140 if user_signed_in?
141 'partial_new_notation'
142 else
143 t('join_our_javanese_community', gending_name: @gending.name)
144 end
145 end
146
147 def notation_info
148 @author = @gending.user_or_nowhereman
149 @page_title = t('gending_notation_page_title', title: @gending.name)
150 @meta_title = @page_title
151 @meta_description = t('gending_notation_meta_desciption', title: @gending.name)
152 @gending.increment!(:views_count) unless browser.bot?
153 end
154 def javanese_gending_params
155 params
156 .require(:javanese_gending)
157 .permit(:name, :description, :notation)
158 end
159
160 def page(slug)
161 @page = Page
162 .select(:page_content, :title)
163 .where(page_slug: slug, locale: [I18n.locale, I18n.default_locale])
164 .last
165 end
166
167 def meta_tags(slug)
168 meta = Meta
169 .select(:title, :description)
170 .where(home: slug, locale: [I18n.locale, I18n.default_locale])
171 .last
172
173 @meta_title = meta.title if meta
174 @meta_description = meta.description if meta
175 end
176 end