Skip to content

Commit

Permalink
[fix] CSS for userpages and stats
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence33 committed Jul 14, 2021
2 parents b8a54e3 + 9c262ae commit be84618
Show file tree
Hide file tree
Showing 16 changed files with 357 additions and 47 deletions.
Binary file added app/assets/fonts/inkle-Baskerville.woff
Binary file not shown.
Binary file added app/assets/fonts/inkle-Baskerville.woff2
Binary file not shown.
Binary file added app/assets/fonts/writer-Baskerville.woff
Binary file not shown.
Binary file added app/assets/fonts/writer-Baskerville.woff2
Binary file not shown.
65 changes: 65 additions & 0 deletions app/assets/javascripts/admin/adminpages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var months = {"1":"January", "2":"February", "3":"March", "4":"April", "5":"May", "6":"June", "7":"July", "8":"August", "9":"September", "10":"October", "11":"November", "12":"December"};
var days = {"1":"31", "2":"28", "3":"31", "4":"30", "5":"31", "6":"30", "7":"31", "8":"31", "9":"30", "10":"31", "11":"30", "12":"31"}

document.addEventListener('DOMContentLoaded', function() {

document.querySelector('#minmonth').addEventListener('input', function(){
// need handle bissextile years
document.querySelector('#minday').max = days[this.value];
});

document.querySelectorAll('.search-elem').forEach( function(element) {
element.addEventListener("change", function(){
setTimeout(doSearch,400);
})
});

document.querySelectorAll('.search-elem-text').forEach( function(element) {
element.addEventListener("keyup", function(){
setTimeout(doSearch,400);
})
});

function doSearch(){
fetch('/admin/score_search', {
method: "POST",
credentials: 'include',
headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json'
},
body: JSON.stringify({
rating_search: {
minday: document.querySelector("#minday").value,
minmonth: document.querySelector("#minmonth").value,
minyear: document.querySelector("#minyear").value,
maxday: document.querySelector("#maxday").value,
maxmonth: document.querySelector("#maxmonth").value,
maxyear: document.querySelector("#maxyear").value,
minwords: document.querySelector("#minwords").value,
maxwords: document.querySelector("#maxwords").value
}
})

}).then(r => r.json())
.then(r => {
console.log(r.message);
if(r["story_selection"]){
document.querySelector('#search-results').innerHTML = "";
r.story_selection.forEach( function(s) {
document.querySelector('#search-results').innerHTML +=
`<p>
<span class="id">${s[0]}</span>
<span class="title">${s[1]}</span>
<span class="author">${s[2]}</span>
<span class="email">${s[3]}</span>
<span class="score">${s[4]}</span>
</p>
`
});
};
});

};

}, false);
52 changes: 51 additions & 1 deletion app/assets/stylesheets/admin/adminpages.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ body {

#index {

padding: 10px;

h1 {}
h2 {
margin: 0;
font-size: 1.2em;
}
table {
table#stats {
th {
padding: 10px;
text-align: center;
Expand All @@ -36,4 +38,52 @@ body {
text-align: center;
}
}

table#search {
td:nth-child(1) {
padding: 10px;
width: 80px;
text-align: center;
}
td:nth-child(2) {
width: 200px;
}
input {
width: 120px;
align: left;
}
output {
width: 200px;
}


}

#search-results {
span {
display: inline-block;
overflow: hidden
}
.id {
width: 40px;
color: blue;
font-weight: bold;
}
.title {
width: 180px;
font-weight: bold;
}
.author {
width: 140px;
}
.email {
width: 220px;
}
.score {
width: 220px;
color: #c39;
font-weight: bold;
}

}
}
22 changes: 21 additions & 1 deletion app/controllers/admin/adminpages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@ def index
@current_year = Time.now.year.to_i
@last_three_years = {@current_year.to_s => stories_created_monthly_in(@current_year), (@current_year-1).to_s => stories_created_monthly_in(@current_year-1), (@current_year-2).to_s => stories_created_monthly_in(@current_year-2) }

end
@first_story = Story.first
@last_story = Story.last

end

def score_search
# https://stackoverflow.com/questions/42686139/strong-parameters-permit-an-array-of-symbols
if %i( minwords maxwords minday maxday minmonth maxmonth minyear maxyear).all? { |key| params[:rating_search][key].present? }

subselection = Story.joins(:story_stat).where(created_at: Date.civil(params[:rating_search][:minyear].to_i, params[:rating_search][:minmonth].to_i, params[:rating_search][:minday].to_i)..Date.civil(params[:rating_search][:maxyear].to_i, params[:rating_search][:maxmonth].to_i, params[:rating_search][:maxday].to_i), story_stats: {total_words: params[:rating_search][:minwords].to_i..params[:rating_search][:maxwords].to_i}).order(score: :desc).first(10)
subselection_ids = subselection.map {|e| [e.id, e.title, e.data["editorData"]["authorName"], e.user.email, e.story_stat.score]}
if subselection_ids.present?
message = "Search successful. Hourray some results have been found"
else
message = "Search successful. Yet it returned no record."
end
render json: {message: message, story_selection: subselection_ids}, status: 200
else
render json: {message: "search failed. No sufficient params"}, status: 400
end
end

private

Expand All @@ -30,4 +48,6 @@ def stories_created_monthly_in(year)
end
return monthly_stories
end


end
36 changes: 1 addition & 35 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def update
if params[:data].present? and params[:title].present?
@story.data = params[:data]
@story.title = params[:title]
@story.save

# process_stats(@story)
# process_rating(@story)
@story.save

render json: { message: "ok" }, :status => 201
else
Expand Down Expand Up @@ -84,37 +81,6 @@ def destroy

private

# def process_stats(story_record)
# if story_record.story_stat.present?
# @s = @story.story_stat

# else
# @s = @story.build_story_stat
# end

# stat_results = Stats::Story.new(story_record).stats

# stat_results.each do |k,v|
# @s[k]=v
# end

# @s.save

# end

# def process_rating(story_record)

# if story_record.story_stat.present?
# @s = @story.story_stat
# ratings = Rating::S_m_l_rating.new(story_record).calc
# ratings.each do |k,v|
# @s[k]=v
# end
# @s.save
# end

# end

# The 4 methods below help to recursively build the story preview in show action

def find_chain(current_stitch, story_diverts)
Expand Down
136 changes: 133 additions & 3 deletions app/models/story.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,85 @@
class Story < ApplicationRecord
after_create :assign_url_key
after_save :process_stats
after_save :process_rating

after_create :assign_url_key
after_save :process_stats, if: :data_is_present
after_save :process_rating, if: :data_is_present

before_save :sanitize_title
before_save :sanitize_author
# before_save :sanitize_stitches

belongs_to :user
has_one :story_stat, dependent: :destroy

validates :data, presence: true


def sanitize_s

if self.data.present?
nh = {}
olds = self.data

olds.each do |k1,v1|
if k1 == "stitches"
nh[k1] = {}

v1.each do |k2,v2|
nh[k1][k2] = {}

v2.each do |k3,v3|

if k3 == "content"
cont = []
v3.each do |elem|
if elem.is_a?String
st = ActionController::Base.helpers.sanitize(elem)
cont << st
elsif elem.is_a?Hash
sh = {}
elem.each do |k4,v4|
if k4 == "option"
sh[k4] = ActionController::Base.helpers.sanitize(v4)
else
sh[k4] = v4
end
end
cont << sh
else
cont << elem
end
end
nh[k1][k2][k3] = cont
else
nh[k1][k2][k3] = v3
end

end

end
else
nh[k1] = v1
end
end
return nh
else
return {}
end

end


private

def assign_url_key
self.url_key = self.id
self.save
end

def data_is_present
self.data.present?
end

def process_stats
unless self.story_stat.present?
self.build_story_stat
Expand Down Expand Up @@ -41,4 +107,68 @@ def process_rating

end

def sanitize_title
if self.title.present?
self.title = ActionController::Base.helpers.sanitize(self.title)
end
end

def sanitize_author
if self.data["editorData"]["authorName"].present?
self.data["editorData"]["authorName"] = ActionController::Base.helpers.sanitize(self.data["editorData"]["authorName"])
end
end

def sanitize_stitches

if self.data.present?
nh = {}
olds = self.data

olds.each do |k1,v1|
if k1 == "stitches"
nh[k1] = {}

v1.each do |k2,v2|
nh[k1][k2] = {}

v2.each do |k3,v3|

if k3 == "content"
cont = []
v3.each do |elem|
if elem.is_a?String
st = ActionController::Base.helpers.sanitize(elem)
cont << st
elsif elem.is_a?Hash
sh = {}
elem.each do |k4,v4|
if k4 == "option"
sh[k4] = ActionController::Base.helpers.sanitize(v4)
else
sh[k4] = v4
end
end
cont << sh
else
cont << elem
end
end
nh[k1][k2][k3] = cont
else
nh[k1][k2][k3] = v3
end

end

end
else
nh[k1] = v1
end
end
self.data = nh
end

end

end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable, :rememberable, :recoverable

has_many :stories, dependent: :destroy
has_one :admin
has_one :admin, dependent: :destroy


validates :email, presence: true
Expand Down
Loading

0 comments on commit be84618

Please sign in to comment.