Skip to content

Commit

Permalink
working db changes, form, policy
Browse files Browse the repository at this point in the history
  • Loading branch information
oma-s committed Apr 5, 2024
1 parent cf9a837 commit abe4234
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 5 deletions.
35 changes: 35 additions & 0 deletions app/controllers/daily_nerd_messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class DailyNerdMessagesController < ApplicationController
before_action :authenticate_user!
before_action :assign_daily_nerd_message, only: [:update]

def create
@daily_nerd_message = authorize DailyNerdMessage.new(daily_nerd_message_attributes.merge(sprint_feedback:))
if @daily_nerd_message.save
@daily_nerd_message.push_to_slack
redirect_to sprints_path
else
render "new", status: :unprocessable_entity
end
end

def update
@daily_nerd_message.update!(daily_nerd_message_attributes)
redirect_to sprints_path
end

private

def daily_nerd_message_attributes
params.require(:daily_nerd_message).permit(:message)
end

def assign_daily_nerd_message
@daily_nerd_message = authorize DailyNerdMessage.find(params[:id])
end

def sprint_feedback
@sprint_feedback ||= current_user.sprint_feedbacks.find_by(sprint: Sprint.current.take)
end
end
4 changes: 3 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class PagesController < ApplicationController
def home
@payslips = current_user.payslips.reverse_chronologic.page(0).per(6)
@sprint = Sprint.current.take
@upcoming_leaves = current_user.leaves.future.chronologic
@upcoming_leaves = current_user.leaves.future.not_rejected.chronologic
sprint_feedback = current_user.sprint_feedbacks.find_by(sprint: @sprint)
@daily_nerd_message = authorize DailyNerdMessage.find_by(created_at: Time.zone.today.all_day, sprint_feedback:) || sprint_feedback.daily_nerd_messages.build if sprint_feedback
end

def offline
Expand Down
19 changes: 19 additions & 0 deletions app/models/daily_nerd_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: daily_nerd_messages
#
# id :uuid not null, primary key
# sprint_feedback_id :uuid not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
#
class DailyNerdMessage < ApplicationRecord
belongs_to :sprint_feedback

def push_to_slack
User::SlackNotification.new(sprint_feedback.user).post_daily_nerd_message(message)
end
end
1 change: 1 addition & 0 deletions app/models/leave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Leave < ApplicationRecord
scope :future, -> { where("UPPER(leaves.leave_during) > NOW()") }
scope :with_status, ->(status) { (status == :all) ? all : where(status:) }
scope :starts_today, -> { where("LOWER(leaves.leave_during) = ?", Time.zone.today) }
scope :not_rejected, -> { where.not(status: :rejected) }

enum type: [:paid, :unpaid, :sick, :non_working].index_with(&:to_s)
enum status: [:pending_approval, :approved, :rejected].index_with(&:to_s)
Expand Down
20 changes: 18 additions & 2 deletions app/models/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ def notify(channel:, text:)
request http_method: :post, slack_method: "chat.postMessage", body: {channel:, text:}.to_json
end

def push_personalized_message_to_daily_nerd_channel(body:)
request_hook url: Config.slack_webhook_url!, body: body.to_json
end

def retrieve_users_slack_id_by_email(email)
response = request http_method: :get, slack_method: "users.lookupByEmail", query: {email:}
response.dig("user", "id")
end

def retrieve_users_profile_image_url_by_email(email)
response = request http_method: :get, slack_method: "users.lookupByEmail", query: {email:}
response.dig("user", "profile", "image_72")
end

def set_status(slack_id:, emoji:, text:, until_time:)
return @last_slack_status_update = Status.new(slack_id:, text:, until_time:) if debug

Expand All @@ -31,10 +40,17 @@ def set_status(slack_id:, emoji:, text:, until_time:)
def request(http_method:, slack_method:, query: nil, body: nil, token_type: :bot)
token = Config.public_send("slack_#{token_type}_token!")
headers = {"Content-Type": "application/json", authorization: "Bearer #{token}"}
response = HTTParty.public_send(http_method, "https://slack.com/api/#{slack_method}", headers:,
query:, body:)
response = HTTParty.public_send(http_method, "https://slack.com/api/#{slack_method}", headers:, query:, body:)

raise NetworkError, response["error"].humanize unless response.ok?

response
end

def request_hook(url:, body:)
response = HTTParty.post(url, headers: {"Content-Type": "application/json"}, body:)
raise NetworkError, response unless response.ok?

response
end
end
2 changes: 2 additions & 0 deletions app/models/sprint_feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SprintFeedback < ApplicationRecord
belongs_to :sprint
belongs_to :user

has_many :daily_nerd_messages, dependent: :destroy

scope :ordered, -> { joins(:user).order("users.email ASC") }

before_validation do
Expand Down
10 changes: 10 additions & 0 deletions app/models/user/slack_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def send_message(message)
Slack.instance.notify(channel: slack_id, text: message)
end

def post_daily_nerd_message(message)
body = {
username: user.display_name,
icon_url: user.slack_profile.image_url,
text: message
}

Slack.instance.push_personalized_message_to_daily_nerd_channel(body:)
end

private

def slack_id
Expand Down
4 changes: 4 additions & 0 deletions app/models/user/slack_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ def ensure_slack_id!
user.update!(slack_id: id)
id
end

def image_url
Slack.instance.retrieve_users_profile_image_url_by_email(user.email)
end
end
end
19 changes: 19 additions & 0 deletions app/policies/daily_nerd_message_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class DailyNerdMessagePolicy < ApplicationPolicy
def home?
hr? || users_own_message?
end

def create?
hr? || users_own_message?
end

def update?
hr? || users_own_message?
end

def users_own_message?
user == record.sprint_feedback.user
end
end
3 changes: 3 additions & 0 deletions app/views/daily_nerd_messages/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= form_with model: @daily_nerd_message, class: :stack, builder: AwesomeForm do |f|
= f.input :message, as: :text, placeholder: t(".message_placeholder")
span = f.submit class: :button
1 change: 1 addition & 0 deletions app/views/daily_nerd_messages/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render "form"
1 change: 1 addition & 0 deletions app/views/daily_nerd_messages/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render "form"
8 changes: 8 additions & 0 deletions app/views/pages/home.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
h1.headline = t ".hello", name: current_user.display_name
- if @sprint
= render @sprint
- if @daily_nerd_message
.card
.card__header
.card__icon 📝
.card__header-content
.card__title = t ".daily_nerd"
.stack
= render "daily_nerd_messages/form"
.columns
- if @upcoming_leaves.any?
.card
Expand Down
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8g0slLgy60K8tQLudFHNQKKy1hHtKlHA9ltjc45wVSN7qrB+qufXavh6szdM3NMUPC8C9V8jKnC8+fYG8YBGiNGIAUubccFtaITGUGxCQQ6hnrxZMVe+fIaXBoY3zxnGbpML/O+EEoD9ULnHJU+o/3Gzpd/QkEFEwK8f3bJGBJTkMkVo6NzEP6WF0e3p7G3NYaRxVTClkz2DZ/2DLuv/wXJ+8oTrGy7+0+nzYRkZoPQ/RNnSTn25uHlX1ssz0UNtqUQ4u0QhlMPb4yph1GXJSo4gFWfaxNGlDOctP5CDuswF+rdQXfop7a98uFV9uxz/vP87LGs39550R5+I6bPtPpKQedEBZQLOnE82C/TAiKxJJgrGCmmijkSK7p3Ie0WauYdAFonbzv+Jt4ckvmUCqYu3S59Vd+T9UKQ8Q3VKn7bMN2WAFDln/lOT+UFgZ/AfOwkzLGQ8vikC7syzEXsZlnngmlMzbOWznYN6z085ZulbSnxZhTYu+pKCP+VsvuHi/2kMxv+5J4CMezmFQqLTLCfJ24TJIlcKUPe3LEAAilHLm73jMmy6t86RZgq6BUkaBjxOKp/HNMq4ySScMNwyMe9bdcTDJSZro8smM717cCxOZfn/PomuAw0YZk1vfYi2jRBI/saVtBrVfUpuOkOtfw0VxxzGARWMEDK/g0a9X7TSkjvzaQhvpv1XiWRM0p8o85EUC8ThNxo3Ln5iw090vD4oR2lbA8UA/EYNhpKEZsQimsfh8vjq/ZB62PyPhyEijJo8VRlekcMHx3k9hxsKTIdp+lK0aa9eiwu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ff line number Diff line change
@@ -1,5 +1,8 @@
---
en:
daily_nerd_messages:
form:
message_placeholder: How was your day? What did you learn?
date:
formats:
month_long: "%B"
Expand Down Expand Up @@ -45,6 +48,7 @@ en:
pages:
home:
archive: Archive
daily_nerd: Daily Nerd
hello: Hello %{name}
last_payments: Last payments
remaining_holidays: Remaining holidays
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace :feed do
resources :leaves, only: :index
end
resources :daily_nerd_messages, only: [:create, :update]
root "pages#home"
end

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240404112633_create_daily_nerd_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDailyNerdMessage < ActiveRecord::Migration[7.0]
def change
create_table :daily_nerd_messages, id: :uuid do |t|
t.references :sprint_feedback, null: false, foreign_key: true, type: :uuid
t.string :message, null: false

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit abe4234

Please sign in to comment.