show mood log day by day

This commit is contained in:
Christophe Robillard 2024-06-02 20:19:02 +02:00
parent 107103f226
commit 90bae72b87
12 changed files with 301 additions and 50 deletions

1
.rspec Normal file
View file

@ -0,0 +1 @@
--require spec_helper

View file

@ -2,6 +2,7 @@
source "https://rubygems.org"
gem 'foreman'
gem 'sinatra'
gem 'puma'
gem "sinatra-activerecord"
@ -11,3 +12,5 @@ gem "rake"
gem "rackup", "~> 2.1"
gem "sinatra-contrib", "~> 4.0"
gem "timecop", "~> 0.9.8"

View file

@ -1,49 +1,48 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (7.1.3)
activesupport (= 7.1.3)
activerecord (7.1.3)
activemodel (= 7.1.3)
activesupport (= 7.1.3)
activemodel (7.2.1)
activesupport (= 7.2.1)
activerecord (7.2.1)
activemodel (= 7.2.1)
activesupport (= 7.2.1)
timeout (>= 0.4.0)
activesupport (7.1.3)
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
base64 (0.2.0)
bigdecimal (3.1.6)
concurrent-ruby (1.2.3)
bigdecimal (3.1.8)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
drb (2.2.0)
ruby2_keywords
i18n (1.14.1)
drb (2.2.1)
foreman (0.88.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
minitest (5.22.2)
logger (1.6.1)
minitest (5.25.1)
multi_json (1.15.0)
mustermann (3.0.0)
mustermann (3.0.3)
ruby2_keywords (~> 0.0.1)
mutex_m (0.2.0)
nio4r (2.7.0)
puma (6.4.2)
nio4r (2.7.3)
puma (6.4.3)
nio4r (~> 2.0)
rack (3.0.9)
rack (3.1.7)
rack-protection (4.0.0)
base64 (>= 0.1.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rake (13.1.0)
rake (13.2.1)
ruby2_keywords (0.0.5)
securerandom (0.3.1)
sinatra (4.0.0)
mustermann (~> 3.0)
rack (>= 3.0.0, < 4)
@ -59,34 +58,43 @@ GEM
rack-protection (= 4.0.0)
sinatra (= 4.0.0)
tilt (~> 2.0)
sqlite3 (1.7.2-aarch64-linux)
sqlite3 (1.7.2-arm-linux)
sqlite3 (1.7.2-arm64-darwin)
sqlite3 (1.7.2-x86-linux)
sqlite3 (1.7.2-x86_64-darwin)
sqlite3 (1.7.2-x86_64-linux)
tilt (2.3.0)
sqlite3 (2.0.4-aarch64-linux-gnu)
sqlite3 (2.0.4-aarch64-linux-musl)
sqlite3 (2.0.4-arm-linux-gnu)
sqlite3 (2.0.4-arm-linux-musl)
sqlite3 (2.0.4-arm64-darwin)
sqlite3 (2.0.4-x86-linux-gnu)
sqlite3 (2.0.4-x86-linux-musl)
sqlite3 (2.0.4-x86_64-darwin)
sqlite3 (2.0.4-x86_64-linux-gnu)
sqlite3 (2.0.4-x86_64-linux-musl)
tilt (2.4.0)
timecop (0.9.10)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webrick (1.8.1)
PLATFORMS
aarch64-linux
arm-linux
aarch64-linux-gnu
aarch64-linux-musl
arm-linux-gnu
arm-linux-musl
arm64-darwin
x86-linux
x86-linux-gnu
x86-linux-musl
x86_64-darwin
x86_64-linux
x86_64-linux-gnu
x86_64-linux-musl
DEPENDENCIES
foreman
puma
rackup (~> 2.1)
rake
sinatra
sinatra-activerecord
sinatra-contrib (~> 4.0)
sqlite3
timecop (~> 0.9.8)
BUNDLED WITH
2.5.4
2.5.3

1
Procfile Normal file
View file

@ -0,0 +1 @@
web: ruby mood_app.rb

27
mood.rb Normal file
View file

@ -0,0 +1,27 @@
class Mood < ActiveRecord::Base
validates_presence_of :mode
validates_presence_of :recorded_at
def self.log
return if Mood.count < 1
first_mood = Mood.first.recorded_at.to_date
first_monday = first_mood - (first_mood.wday - 1)
current_date = first_monday
current_mode = nil
log_mood = []
Mood.all.each do |mood|
while current_date < mood.recorded_at.to_date do
log_mood << [ current_date.to_s, current_mode ]
current_date += 1
end
current_mode = mood.mode
end
while current_date <= Date.today do
log_mood << [ current_date.to_s, current_mode ]
current_date += 1
end
log_mood.each_slice(7).to_a
end
end

View file

@ -1,14 +1,16 @@
require 'sinatra/base'
require 'sinatra/namespace'
require 'sinatra/activerecord'
require './mood'
class MoodApp < Sinatra::Base
register Sinatra::ActiveRecordExtension
register Sinatra::Namespace
set :database, { adapter: 'sqlite3', database: 'mood.sqlite3' }
set :database, { adapter: 'sqlite3', database: "mood-#{ENV['RACK_ENV']}.sqlite3" }
get '/' do
@last_mood = Mood.order(recorded_at: :desc).first
@mood_log = Mood.log
erb :index
end
@ -27,8 +29,3 @@ class MoodApp < Sinatra::Base
run! if app_file == $0
end
class Mood < ActiveRecord::Base
validates_presence_of :mode
validates_presence_of :recorded_at
end

18
mood_app_spec.rb Normal file
View file

@ -0,0 +1,18 @@
ENV['APP_ENV'] = 'test'
require './mood_app'
require 'rspec'
require 'rack/test'
describe 'MoodApp' do
include Rack::Test::Methods
it 'says hello' do
get '/'
expect(last_response).to be_ok
end
def app
MoodApp
end
end

42
mood_spec.rb Normal file
View file

@ -0,0 +1,42 @@
ENV['APP_ENV'] = 'test'
require './mood_app'
require 'rspec'
require 'timecop'
describe 'Mood' do
before do
Mood.destroy_all
Timecop.freeze(Time.local(2024,2,10,9))
end
after do
Timecop.return
end
it 'returns log' do
Mood.create(recorded_at: '2024-02-03', mode: 'creatif')
Mood.create(recorded_at: '2024-02-09', mode: 'en-charge')
expect(Mood.log).to eq [
[
{'2024-01-29' => nil},
{'2024-01-30' => nil},
{'2024-01-31' => nil},
{'2024-02-01' => nil},
{'2024-02-02' => nil},
{'2024-02-03' => 'creatif'},
{'2024-02-04' => 'creatif'}
],
[
{'2024-02-05' => 'creatif'},
{'2024-02-06' => 'creatif'},
{'2024-02-07' => 'creatif'},
{'2024-02-08' => 'creatif'},
{'2024-02-09' => 'en-charge'},
{'2024-02-10' => 'en-charge'}
]
]
end
end

BIN
public/croisiere.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 KiB

BIN
public/croisiere.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

100
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,100 @@
ENV["RACK_ENV"] = 'test'
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

View file

@ -1,14 +1,68 @@
<style>
main {
display: flex;
align-items: center;
justify-content: center;
}
.mode {
max-width: 80vw;
max-height: 80vh;
margin: auto;
height: 100vh;
}
img {
width: auto;
height: 100%;
}
table {
empty-cells: hide;
border-collapse: collapse;
}
tbody tr td {
border: 1px solid black;
border-spacing: 3px;
min-width: 20px;
min-height: 20px;
border-radius: 50%;
margin: 5px;
}
.week {
display: flex;
}
.day {
background-color: white;
border: 1px;
margin: 1px;
min-width: 10px;
min-height: 10px;
}
.creatif {
background-color: purple;
}
.en-charge {
background-color: orange;
}
.frigo-vide {
background-color: brown;
}
.croisiere {
background-color: green;
}
</style>
<div class="mode">
<main>
<div class="mode">
<img src="<%= @last_mood.mode %>.png">
</div>
</div>
<div class="yearlog">
<% @mood_log.each do |week| %>
<div class="week">
<% week.each do |d| %>
<% if d[1] %>
<div title="<%= d[0] %> : <%= d[1] %>" class="day <%= d[1] %>"></div>
<% else %>
<div class="day"></div>
<% end %>
<% end %>
</div>
<% end %>
</div>
</main>