forked from espoo-dev/rails_boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ca04ae
commit 640fa47
Showing
30 changed files
with
28,977 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class SongsIndex < Chewy::Index | ||
index_scope Song | ||
|
||
settings analysis: { | ||
analyzer: { | ||
email: { | ||
tokenizer: "keyword", | ||
filter: ["lowercase"] | ||
} | ||
} | ||
} | ||
|
||
field :title, type: :text | ||
field :artist, type: :text | ||
field :genre, type: :keyword | ||
field :lyrics, type: :text | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
class SongsController < ApplicationController | ||
before_action :set_song, only: %i[ show edit update destroy ] | ||
|
||
# GET /songs or /songs.json | ||
def index | ||
# @songs = Song.all | ||
query = params["query"] || "" | ||
genre = params["genre"] || "" | ||
filters = { keywords: query, genre: genre } | ||
|
||
@songs = Songs::Search.new(filters: filters).search | ||
@songs | ||
end | ||
|
||
# GET /songs/1 or /songs/1.json | ||
def show | ||
end | ||
|
||
# GET /songs/new | ||
def new | ||
@song = Song.new | ||
end | ||
|
||
# GET /songs/1/edit | ||
def edit | ||
end | ||
|
||
# POST /songs or /songs.json | ||
def create | ||
@song = Song.new(song_params) | ||
|
||
respond_to do |format| | ||
if @song.save | ||
format.html { redirect_to song_url(@song), notice: "Song was successfully created." } | ||
format.json { render :show, status: :created, location: @song } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @song.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /songs/1 or /songs/1.json | ||
def update | ||
respond_to do |format| | ||
if @song.update(song_params) | ||
format.html { redirect_to song_url(@song), notice: "Song was successfully updated." } | ||
format.json { render :show, status: :ok, location: @song } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @song.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /songs/1 or /songs/1.json | ||
def destroy | ||
@song.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to songs_url, notice: "Song was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_song | ||
@song = Song.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def song_params | ||
params.require(:song).permit(:title, :artist, :genre, :lyrics) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SongsHelper | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'csv' | ||
|
||
class Song < ApplicationRecord | ||
update_index("songs") { self } | ||
|
||
def self.import_csv! | ||
filepath = "tcc_ceds_music.csv" | ||
res = CSV.parse(File.read(filepath), headers: true) | ||
res.each_with_index do |s, ind| | ||
Song.create!( | ||
artist: s["artist_name"], | ||
title: s["track_name"], | ||
genre: s["genre"], | ||
lyrics: s["lyrics"] | ||
) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Songs | ||
class Search | ||
def initialize(filters:, scope: SongsIndex.all, page: 1, per_page: 5) | ||
@scope = scope | ||
@keywords = filters.fetch(:keywords, "") | ||
@genre = filters.fetch(:genre) | ||
@page = page | ||
@per_page = per_page | ||
end | ||
|
||
def search | ||
apply_filters.objects | ||
end | ||
|
||
private | ||
|
||
attr_reader :keywords, :genre, :scope, :page, :per_page | ||
|
||
def apply_filters | ||
[ | ||
genre_filter, | ||
query_multi_match | ||
].compact.reduce(:merge) | ||
end | ||
|
||
def query_multi_match | ||
scope.query(multi_match: { | ||
query: keywords, | ||
fields: [ :title, :artist, :lyrics ], | ||
operator: :and, | ||
type: :cross_fields | ||
}) if keywords.present? | ||
end | ||
|
||
def genre_filter | ||
scope.query(term: { genre: genre }) if genre.present? | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<%= form_with(model: song) do |form| %> | ||
<% if song.errors.any? %> | ||
<div style="color: red"> | ||
<h2><%= pluralize(song.errors.count, "error") %> prohibited this song from being saved:</h2> | ||
|
||
<ul> | ||
<% song.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div> | ||
<%= form.label :title, style: "display: block" %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :artist, style: "display: block" %> | ||
<%= form.text_field :artist %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :genre, style: "display: block" %> | ||
<%= form.text_field :genre %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :lyrics, style: "display: block" %> | ||
<%= form.text_area :lyrics %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit %> | ||
</div> | ||
<% end %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div id="<%= dom_id song %>"> | ||
<p> | ||
<strong>Title:</strong> | ||
<%= song.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Artist:</strong> | ||
<%= song.artist %> | ||
</p> | ||
|
||
<p> | ||
<strong>Genre:</strong> | ||
<%= song.genre %> | ||
</p> | ||
|
||
<p> | ||
<strong>Lyrics:</strong> | ||
<%= song.lyrics %> | ||
</p> | ||
|
||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! song, :id, :title, :artist, :genre, :lyrics, :created_at, :updated_at | ||
json.url song_url(song, format: :json) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Editing song</h1> | ||
|
||
<%= render "form", song: @song %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Show this song", @song %> | | ||
<%= link_to "Back to songs", songs_path %> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<h1>Songs</h1> | ||
|
||
<div id="songs"> | ||
<% @songs.each do |song| %> | ||
<%= render song %> | ||
<p> | ||
<%= link_to "Show this song", song %> | ||
</p> | ||
<% end %> | ||
</div> | ||
|
||
<%= link_to "New song", new_song_path %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @songs, partial: "songs/song", as: :song |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>New song</h1> | ||
|
||
<%= render "form", song: @song %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Back to songs", songs_path %> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render @song %> | ||
|
||
<div> | ||
<%= link_to "Edit this song", edit_song_path(@song) %> | | ||
<%= link_to "Back to songs", songs_path %> | ||
|
||
<%= button_to "Destroy this song", @song, method: :delete %> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "songs/song", song: @song |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
Chewy.strategy(:atomic) | ||
Chewy.request_strategy = :sidekiq | ||
Chewy.logger = Logger.new($stdout) | ||
Chewy.logger.level = Logger::INFO | ||
Chewy.settings = { | ||
strategy_config: { | ||
delayed_sidekiq: { | ||
latency: 3, | ||
margin: 2, | ||
ttl: 60 * 60 * 24, | ||
reindex_wrapper: lambda { |&reindex| | ||
ActiveRecord::Base.connected_to(role: :reading) { reindex.call } | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateSongs < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :songs do |t| | ||
t.string :title | ||
t.string :artist | ||
t.string :genre | ||
t.text :lyrics | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FactoryBot.define do | ||
factory :song do | ||
title { "MyString" } | ||
artist { "MyString" } | ||
genre { "MyString" } | ||
lyrics { "MyText" } | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the SongsHelper. For example: | ||
# | ||
# describe SongsHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe SongsHelper, type: :helper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Song, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Oops, something went wrong.