Browse Source

prefs: add offset as a user pref

develop v2.4.0
Arthur POULET 3 months ago
parent
commit
0485f8131d
Signed by: Sceptique GPG Key ID: C108A668349EE601
  1. 2
      Gemfile.lock
  2. 6
      README.md
  3. 24
      src/systems/pex.rb
  4. 6
      src/systems/user.rb
  5. 1
      src/systems/user_pex.rb
  6. 17
      src/utils/json_api.rb
  7. 15
      src/utils/users.rb
  8. 18
      src/views/preferences.slim

2
Gemfile.lock

@ -22,7 +22,6 @@ GEM
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nio4r (2.5.7)
pg (1.2.3)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
@ -68,7 +67,6 @@ DEPENDENCIES
doc_my_routes
dotenv (~> 2.7)
jwt (~> 2.2)
pg (~> 1.2)
pry (~> 0.14.1)
puma (~> 5.3)
rack-test (~> 1.1)

6
README.md

@ -95,3 +95,9 @@ LIFEPEX_ENV=test rake db:migrate
```
Then if you want to run the test, simply type `rake test` (you will need the startup env variable to be set first).
### Debug
Some debug options can be enabled with the env variable
`LIFEPEX_ENV=debug`

24
src/systems/pex.rb

@ -1,32 +1,16 @@
require "date"
require "active_support/all"
require_relative "./auth.rb"
require_relative "./csrf.rb"
require_relative "../utils/users.rb"
class LifePex::Systems::PexSystem < LifePex::Systems::AuthSystem
include JSON::API
include LifePex::Systems::CrlfHelper
def my_user_pexs(cookies, date = nil)
params = {
user_id: user_id_decoded(cookies),
}
params[:created_at] = date unless date.nil?
LifePex::UserPex.where(params).all()
end
def get_user_date
date = cookies["date"]
if date && Date.respond_to?(date)
Date.send date
elsif date == "yesterday"
Date.today - 1
else
Date.today
end
end
include LifePex::UsersHelper
get "/today", auth: [] do
cookies.set "date", { value: "today", httponly: false }
cookies.set "date", { value: "now", httponly: false }
redirect "/"
end

6
src/systems/user.rb

@ -5,6 +5,7 @@ require_relative "./csrf.rb"
class LifePex::Systems::UserSystem < LifePex::Systems::AuthSystem
include JSON::API
include LifePex::Systems::CrlfHelper
include LifePex::UsersHelper
DEFAULT_PEXS_FOR_NEW_USERS = YAML.load_file "config/default_pexs_for_new_users.yaml"
@ -97,6 +98,11 @@ class LifePex::Systems::UserSystem < LifePex::Systems::AuthSystem
if !(recap_days = params["inputRecapDays"]).blank?
cookies["recap_days"] = recap_days.to_i
end
if !(late_day_offset = params["inputLateDayOffset"]).blank?
cookies["late_day_offset"] = late_day_offset.to_f
end
show_preferences
end

1
src/systems/user_pex.rb

@ -134,5 +134,6 @@ class LifePex::Systems::UserPexSystem < LifePex::Systems::AuthSystem
end
end
include LifePex::Systems::ApiList
end

17
src/utils/json_api.rb

@ -1,3 +1,5 @@
require "active_support/all"
module JSON::API
def json_params
begin
@ -10,18 +12,19 @@ module JSON::API
end
end
def date_input_convertor(date)
if date && Date.respond_to?(date)
Date.send date
elsif date == "yesterday"
Date.today - 1
# @param [String] date: either a date iso formatted or a word to be sent to DateTime
# @param [Float] offset: an amount of hours to remove from the date, useful for setting the start of the day hours after midnight
def date_input_convertor(date = "now", offset = 0.0)
base_time = if date && DateTime.respond_to?(date)
DateTime.send date
else
begin
Date.parse date
DateTime.parse date
rescue => _
Date.today
DateTime.now
end
end
(base_time - offset.hours).to_date
end
# params:

15
src/utils/users.rb

@ -0,0 +1,15 @@
module LifePex::UsersHelper
def my_user_pexs(cookies, date = nil)
params = {
user_id: user_id_decoded(cookies),
}
params[:created_at] = date unless date.nil?
LifePex::UserPex.where(params).all()
end
def get_user_date
date = cookies["date"]
offset = cookies["late_day_offset"].to_f
date_input_convertor(date, offset)
end
end

18
src/views/preferences.slim

@ -11,9 +11,27 @@
strong
| Days to show by default
input#inputUsername.form-control.form-control-lg name="inputRecapDays" type="text" value=cookies["recap_days"] /
.form-group.row
label.col-sm-12.col-form-label for="inputLateDayOffset"
strong
| Offset for today (so a few hours after midnight is still today)
input#inputUsername.form-control.form-control-lg name="inputLateDayOffset" type="number" min="0" max="24" step="0.1" value=cookies["late_day_offset"] /
.form-group.row
input.btn.btn-lg.btn-block type="submit" value="Update"
- if LifePex::APP_ENV == "debug"
h2
| Debug
p
table
- cookies.each do |k, v|
tr
td= "cookies.#{k}"
td= v
tr
td
| get_user_date
td= get_user_date
h2
| Private data export

Loading…
Cancel
Save