Created
November 8, 2017 10:35
-
-
Save PlanetRoast/3145d8b8299fafef57695fd1979b8acb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class JentriesController < ApplicationController | |
def index | |
@jentries = Jentry.all | |
end | |
def show | |
@jentry = Jentry.find_by(:jentry_timestamp => params[:id]) | |
end | |
def ymd | |
@jentries = Jentry.all | |
unless params[:year] == "*" | |
@jentries = @jentries.where(:year => params[:year]) | |
end | |
unless params[:month] == "*" | |
@jentries = @jentries.where(:month => params[:month]) | |
end | |
unless params[:day] == "*" | |
@jentries = @jentries.where(:day => params[:day]) | |
end | |
end | |
def latest | |
@jentry = Jentry.all.order(:jentry_timestamp).first | |
render action: "show" | |
end | |
def last_weeks | |
week_count = params[:weeks].to_i | |
from_date = Time.now - week_count.weeks | |
@jentries = Jentry.where('date > ?', from_date) | |
render :search | |
end | |
def random | |
@jentry = Jentry.all.sample | |
redirect_to jentry_path(@jentry.jentry_timestamp) | |
end | |
def search | |
@q = params[:q] | |
@jentries = Jentry.search_for(@q) | |
end | |
def poi | |
@jentries = JentriesTag.all | |
end | |
def import | |
clear_jentries | |
files = Dir["app/jentry/*.txt"] | |
files.each do |j| | |
puts "**** importing jentry #{j}... *******" | |
create_jentry(j) | |
end | |
update_characters | |
set_all_weekdays | |
update_tags | |
redirect_to action: 'index' | |
end | |
private | |
def create_jentry(j) | |
Jentry.create( | |
filename: j, | |
date: jentry_date(j), | |
jentry_timestamp: jentry_timestamp(j), | |
year: jentry_timestamp(j)[0,2], | |
month: jentry_timestamp(j)[2,2], | |
day: jentry_timestamp(j)[4,2], | |
content: File.read(j), | |
words: File.read(j).split.size | |
) | |
end | |
def update_tags | |
clear_tags # deletes all the tags | |
clear_jentry_tags # deletes all the jentries_tags | |
Jentry.all.each do |j| | |
set_tags(j) | |
end | |
end | |
def set_tags(j) | |
# Yay! Regex! How I love Regex | |
#tags = j.content.scan(/\@\S+/) # old version won't account for double quotes | |
tags = j.content.scan(/@.*"/) # new version searches for ' @" ' and continues until the end of the line | |
if tags.any? | |
tags.each do |tag| | |
tag_name = tag.split("=").first | |
tag_content = tag.split("=").last.tr('"', '') | |
create_jentry_tag(j, tag_name, tag_content) | |
end | |
end | |
end | |
def create_jentry_tag(j, tag_name, tag_content) | |
puts "Creating jentry tag for #{tag_name} with content: #{tag_content}." | |
if tag_name.include?("networth") | |
#puts "***** found networth tag #{tag_content}" | |
handle_networth_tag(j, tag_content) | |
else | |
JentriesTag.create(jentry_id: j.id, tag_id:set_tag(tag_name).id) | |
end | |
end | |
# Networth has its own method because it's saved against the Jentry. | |
def handle_networth_tag(j, tag_content) | |
j.update(:networth => tag_content) | |
end | |
def set_tag(name) | |
Tag.where(name: name).first_or_create | |
end | |
def jentry_timestamp(filename) | |
filename.split("/").last.split(".").first | |
end | |
def jentry_date(filename) | |
DateTime.strptime(jentry_timestamp(filename), "%y%m%d%H%M") | |
end | |
def clear_jentries | |
Jentry.destroy_all | |
end | |
def update_characters | |
Jentry.all.each do |j| | |
j.characters = j.content.length | |
j.save | |
end | |
end | |
def set_all_weekdays | |
Jentry.all.each do |j| | |
set_weekday(j) | |
end | |
end | |
def set_weekday(j) | |
j.weekday = j.date.wday | |
j.save | |
end | |
def update_filenames | |
files = Dir["app/jentry/20*.txt"] | |
files.each do |f| | |
old_name = f | |
new_name = f.gsub("-", "").gsub(" ", "").gsub("/20", "/") | |
File.rename(old_name, new_name) | |
puts new_name | |
end | |
end | |
def clear_tags | |
Tag.destroy_all | |
end | |
def clear_jentry_tags | |
JentriesTag.destroy_all | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment