Skip to content

Commit

Permalink
Enable Assistant ownership (#62)
Browse files Browse the repository at this point in the history
* add assistant owner

* set default

* tests
  • Loading branch information
vswamidass-sfdc authored Sep 5, 2024
1 parent c4b2395 commit 82dcfb9
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/controllers/assistants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def show; end
# GET /assistants/new
def new
@assistant = Assistant.new
@assistant.user_id = current_user.id
end

# GET /assistants/1/edit
Expand Down Expand Up @@ -68,6 +69,6 @@ def set_assistant

# Only allow a list of trusted parameters through.
def assistant_params
params.require(:assistant).permit(:libraries, :name, :input, :output, :context, :instructions, :description, :status, :quip_url, :confluence_spaces)
params.require(:assistant).permit(:libraries, :name, :input, :output, :context, :instructions, :description, :status, :quip_url, :confluence_spaces, :user_id)
end
end
1 change: 1 addition & 0 deletions app/models/assistant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Assistant < ApplicationRecord
has_many :chats
belongs_to :user

enum status: { development: 0, ready: 1 }

Expand Down
2 changes: 1 addition & 1 deletion app/policies/assistant_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create?
end

def update?
user.admin? || user.editor?
user.admin? || user.editor? || assistant.user_id == user.id
end

def destroy?
Expand Down
4 changes: 4 additions & 0 deletions app/views/assistants/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<%= form.label :status %>
<%= form.select :status, Assistant.statuses.keys.map { |status| [status.humanize, status] }, {}, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :user_id, 'Owner' %>
<%= form.collection_select :user_id, User.all.order(email: :asc), :id, :email, {include_blank: 'Select a User'}, {class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"} %>
</div>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
Expand Down
16 changes: 11 additions & 5 deletions app/views/assistants/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
</div>
<div id="assistants" class="mt-5 space-y-4">
<% @assistants.each do |assistant| %>
<div class="flex justify-between items-center bg-white rounded-lg shadow-lg p-4">
<div class="flex justify-between items-center bg-white rounded-lg shadow-lg p-4 border-stone-200 border">
<div class="w-5/6">
<h2 class="font-bold text-xl mb-2 flex items-center ">
<%= link_to assistant.name, assistant, class: "text-sky-500 hover:underline ml-1" %>
<h2 class="text-xl flex items-center ">
<%= link_to assistant.name, assistant, class: "text-sky-500 hover:underline" %>
<%= render partial: 'assistant_badge', locals: { assistant: assistant } %>
</h2>
<p class="text-gray-500 text-sm"><%= assistant.description %></p>
<p class="text-stone-600 text-sm"><%= assistant.description %></p>
<%= render partial: 'shared/meta_info', locals: { email: assistant.user ? assistant.user.email : "admin", created_at: assistant.created_at, updated_at: assistant.updated_at } %>
</div>
<div class="">
<%= link_to "Start Chat", new_assistant_chat_path(assistant_id: assistant.id), class: "rounded-lg py-2 px-4 bg-white text-sky-500 border border-sky-500 font-medium hover:bg-sky-100" %>
<%= link_to new_assistant_chat_path(assistant_id: assistant.id), class: "rounded-lg py-2 px-4 bg-white text-sky-500 border border-sky-500 font-medium hover:bg-sky-100 flex items-center space-x-2" do %>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 0 1 1.037-.443 48.282 48.282 0 0 0 5.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0 0 12 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018Z" />
</svg>
<span>Chat</span>
<% end %>
</div>
</div>
<% end %>
Expand Down
12 changes: 8 additions & 4 deletions app/views/assistants/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
</ul>
</nav>
<div class="flex justify-between items-center">
<h2 class="text-3xl font-bold mb-4 flex items-center">
<%= @assistant.name %>
<span class="ml-2 inline-flex"> <%= render partial: 'assistant_badge', locals: { assistant: @assistant } %></span>
</h2>
<div>
<h2 class="text-3xl font-bold flex items-center">
<%= @assistant.name %>
<span class="ml-2 inline-flex"> <%= render partial: 'assistant_badge', locals: { assistant: @assistant } %></span>
</h2>
<%= render partial: 'shared/meta_info', locals: { email: @assistant.user ? @assistant.user.email : "admin", created_at: @assistant.created_at, updated_at: @assistant.updated_at } %>

</div>
<div>
<%= link_to new_assistant_chat_path(@assistant), class: "rounded-lg py-3 px-5 bg-sky-800 inline-flex items-center font-medium text-white" do %>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 mr-2">
Expand Down
5 changes: 5 additions & 0 deletions app/views/shared/_meta_info.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="py-2 text-stone-500 text-xs ">
Owner <span class="text-stone-500 font-bold "><%= email %> </span> |
Created <span class="text-stone-500 font-bold "><%= time_ago_in_words(created_at) %> ago</span> |
Updated <span class="text-stone-500 font-bold "><%= time_ago_in_words(updated_at) %> ago</span>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20240905173708_add_owner_to_assistant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOwnerToAssistant < ActiveRecord::Migration[7.1]
def change
add_reference :assistants, :user, null: true, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

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

2 changes: 1 addition & 1 deletion spec/controllers/chats_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Assuming the Assistant model requires these fields
let(:library) { Library.create!(name: 'Test Library', user:) }
let(:assistant) { Assistant.create!(name: 'Test Assistant', libraries: '1,2', input: 'Sample input', instructions: 'Sample instructions', output: 'Sample output') }
let(:assistant) { Assistant.create!(name: 'Test Assistant', user:, libraries: '1,2', input: 'Sample input', instructions: 'Sample instructions', output: 'Sample output') }

let(:valid_attributes) { { first_message: 'Hello', assistant_id: assistant.id } }
let(:invalid_attributes) { { first_message: '', assistant_id: nil } }
Expand Down
4 changes: 3 additions & 1 deletion spec/models/assistant_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rails_helper'

RSpec.describe Assistant, type: :model do
let(:user) { create(:user) }

describe 'validations' do
it { should validate_presence_of(:libraries) }
it { should validate_presence_of(:input) }
Expand All @@ -9,7 +11,7 @@

context 'libraries CSV validation' do
it 'is valid with a valid CSV string of numbers' do
assistant = Assistant.new(libraries: '1,2,3.5,4', input: 'input', instructions: 'instructions', output: 'output')
assistant = Assistant.new(libraries: '1,2,3.5,4', input: 'input', user:, instructions: 'instructions', output: 'output')
expect(assistant).to be_valid
end

Expand Down
1 change: 1 addition & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
input: 'Sample input',
instructions: 'Sample instructions',
output: 'Sample output',
user:,
libraries: '1,2,3') # Assuming libraries expects a CSV of numbers
end

Expand Down

0 comments on commit 82dcfb9

Please sign in to comment.