Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organization index and show #5187

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/images/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions app/controllers/organizations/gems_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Organizations::GemsController < ApplicationController
before_action :redirect_to_signin, unless: :signed_in?
before_action :redirect_to_new_mfa, if: :mfa_required_not_yet_enabled?
before_action :redirect_to_settings_strong_mfa_required, if: :mfa_required_weak_level_enabled?

before_action :find_organization, only: %i[index]

layout "subject"

# GET /organizations/organization_id/gems

def index
@gems = @organization.rubygems.with_versions.by_downloads.preload(:most_recent_version, :gem_download).load_async
@gems_count = @organization.rubygems.with_versions.count

Check warning on line 14 in app/controllers/organizations/gems_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/gems_controller.rb#L13-L14

Added lines #L13 - L14 were not covered by tests
end

private

def find_organization
@organization = Organization.find_by_handle!(params[:organization_id])

Check warning on line 20 in app/controllers/organizations/gems_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/gems_controller.rb#L20

Added line #L20 was not covered by tests
end
end
66 changes: 66 additions & 0 deletions app/controllers/organizations/members_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Organizations::MembersController < ApplicationController
before_action :redirect_to_signin, only: :index, unless: :signed_in?
before_action :redirect_to_new_mfa, only: :index, if: :mfa_required_not_yet_enabled?
before_action :redirect_to_settings_strong_mfa_required, only: :index, if: :mfa_required_weak_level_enabled?

before_action :find_organization, only: %i[create update destroy]
before_action :find_membership, only: %i[update destroy]

layout "subject"

def index
@organization = Organization.find_by_handle!(params[:organization_id])
authorize @organization, :list_memberships?

Check warning on line 13 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L12-L13

Added lines #L12 - L13 were not covered by tests

@memberships = @organization.memberships.includes(:user)
@memberships_count = @organization.memberships.count

Check warning on line 16 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L15-L16

Added lines #L15 - L16 were not covered by tests
end

def create
membership = @organization.memberships.new(create_membership_params)
authorize membership

Check warning on line 21 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L20-L21

Added lines #L20 - L21 were not covered by tests

if membership.save
redirect_to organization_members_path(@organization)

Check warning on line 24 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L23-L24

Added lines #L23 - L24 were not covered by tests
else
render :index

Check warning on line 26 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L26

Added line #L26 was not covered by tests
end
end

def update
@membership.attributes = update_membership_params
authorize @membership

Check warning on line 32 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L31-L32

Added lines #L31 - L32 were not covered by tests

if @membership.save
redirect_to organization_members_path(@organization)

Check warning on line 35 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L34-L35

Added lines #L34 - L35 were not covered by tests
else
redirect_to organization_members_path(@organization), error: "Failed to update membership"

Check warning on line 37 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L37

Added line #L37 was not covered by tests
end
end

def destroy
authorize @membership
@membership.destroy

Check warning on line 43 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L42-L43

Added lines #L42 - L43 were not covered by tests

redirect_to organization_members_path(@organization)

Check warning on line 45 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L45

Added line #L45 was not covered by tests
end

private

def find_organization
@organization = Organization.find_by_handle!(params[:organization_id])
authorize @organization, :manage_memberships?

Check warning on line 52 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L51-L52

Added lines #L51 - L52 were not covered by tests
end

def find_membership
@membership = @organization.memberships.find(params.permit(:id).require(:id))

Check warning on line 56 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L56

Added line #L56 was not covered by tests
end

def create_membership_params
params.require(:membership).permit(:user_id, :role)

Check warning on line 60 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L60

Added line #L60 was not covered by tests
end

def update_membership_params
params.require(:membership).permit(:role)

Check warning on line 64 in app/controllers/organizations/members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations/members_controller.rb#L64

Added line #L64 was not covered by tests
end
end
47 changes: 46 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
class OrganizationsController < ApplicationController
before_action :redirect_to_signin, only: :index, unless: :signed_in?
before_action :redirect_to_new_mfa, only: :index, if: :mfa_required_not_yet_enabled?
before_action :redirect_to_settings_strong_mfa_required, only: :index, if: :mfa_required_weak_level_enabled?

before_action :find_organization, only: %i[show edit update]

layout "subject"

# GET /organizations
def index
@memberships = current_user.memberships.includes(:organization)

Check warning on line 12 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L12

Added line #L12 was not covered by tests
end

# GET /organizations/1
def show
render plain: flash[:notice] # HACK: for tests until this view is ready
@latest_events = [] # @organization.latest_events
@gems = @organization

Check warning on line 18 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L17-L18

Added lines #L17 - L18 were not covered by tests
.rubygems
.with_versions
.by_downloads
.preload(:most_recent_version, :gem_download)
.load_async
@gems_count = @organization.rubygems.with_versions.count
@memberships = @organization.memberships
@memberships_count = @organization.memberships.count

Check warning on line 26 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L24-L26

Added lines #L24 - L26 were not covered by tests
end

def edit
add_breadcrumb t("breadcrumbs.org_name", name: @organization.handle), organization_path(@organization)
add_breadcrumb t("breadcrumbs.settings")

Check warning on line 31 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L30-L31

Added lines #L30 - L31 were not covered by tests

authorize @organization

Check warning on line 33 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L33

Added line #L33 was not covered by tests
end

def update
authorize @organization

Check warning on line 37 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L37

Added line #L37 was not covered by tests

if @organization.update(organization_params)
redirect_to organization_path(@organization)

Check warning on line 40 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L39-L40

Added lines #L39 - L40 were not covered by tests
else
render :edit

Check warning on line 42 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L42

Added line #L42 was not covered by tests
end
end

private

def find_organization
@organization = Organization.find_by_handle!(params.permit(:id).require(:id))

Check warning on line 49 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L49

Added line #L49 was not covered by tests
end

def organization_params
params.permit(organization: [:name]).require(:organization)

Check warning on line 53 in app/controllers/organizations_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/organizations_controller.rb#L53

Added line #L53 was not covered by tests
end
end
2 changes: 1 addition & 1 deletion app/controllers/rubygems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@gems = Rubygem.letter(@letter).includes(:latest_version, :gem_download).page(@page)
end
format.atom do
@versions = Version.published.limit(Gemcutter::DEFAULT_PAGINATION)
@versions = Version.published.limit(Gemcutter::DEFAULT_PAGINATION).includes(:rubygem)

Check warning on line 17 in app/controllers/rubygems_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/rubygems_controller.rb#L17

Added line #L17 was not covered by tests
render "versions/feed"
end
end
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/layout_hepler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module LayoutHelper
# <%= layout_section "Footer Nav", class: "py-8 bg-orange-100 dark:bg-orange-950 text-neutral-800 dark:text-neutral-200 flex-col items-center">
def layout_section(_name, **options, &)
options[:class] = "w-full px-8 #{options[:class]}"

Check warning on line 4 in app/helpers/layout_hepler.rb

View check run for this annotation

Codecov / codecov/patch

app/helpers/layout_hepler.rb#L4

Added line #L4 was not covered by tests

tag.div(**options) do
tag.div(class: "max-w-screen-xl mx-auto flex flex-col", &)

Check warning on line 7 in app/helpers/layout_hepler.rb

View check run for this annotation

Codecov / codecov/patch

app/helpers/layout_hepler.rb#L6-L7

Added lines #L6 - L7 were not covered by tests
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/organizations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OrganizationsHelper
end
16 changes: 15 additions & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@
has_many :rubygems, dependent: :nullify
has_one :organization_onboarding, foreign_key: :onboarded_organization_id, inverse_of: :organization, dependent: :destroy

scope :deleted, -> { where.not(deleted_at: nil) }
default_scope { where(deleted_at: nil) }

Check warning on line 16 in app/models/organization.rb

View check run for this annotation

Codecov / codecov/patch

app/models/organization.rb#L16

Added line #L16 was not covered by tests

scope :deleted, -> { unscoped.where.not(deleted_at: nil) }

Check warning on line 18 in app/models/organization.rb

View check run for this annotation

Codecov / codecov/patch

app/models/organization.rb#L18

Added line #L18 was not covered by tests

after_create do
record_event!(Events::OrganizationEvent::CREATED, actor_gid: memberships.first&.to_gid)
end

def self.find_by_handle(handle)
find_by("lower(handle) = lower(?)", handle)
end

Check warning on line 26 in app/models/organization.rb

View check run for this annotation

Codecov / codecov/patch

app/models/organization.rb#L24-L26

Added lines #L24 - L26 were not covered by tests

def self.find_by_handle!(handle)
find_by_handle(handle) || raise(ActiveRecord::RecordNotFound)
end

Check warning on line 30 in app/models/organization.rb

View check run for this annotation

Codecov / codecov/patch

app/models/organization.rb#L28-L30

Added lines #L28 - L30 were not covered by tests

def to_param
handle
end

Check warning on line 34 in app/models/organization.rb

View check run for this annotation

Codecov / codecov/patch

app/models/organization.rb#L32-L34

Added lines #L32 - L34 were not covered by tests
end
2 changes: 2 additions & 0 deletions app/policies/organization_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
organization_member_with_role?(user, :owner) || deny(t(:forbidden))
end

alias edit? update?

Check warning on line 15 in app/policies/organization_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/organization_policy.rb#L15

Added line #L15 was not covered by tests

def create?
true
end
Expand Down
32 changes: 31 additions & 1 deletion app/views/components/card/timeline_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Card::TimelineComponent < ApplicationComponent
include Phlex::Rails::Helpers::LinkTo
include Phlex::Rails::Helpers::ImageTag

Check warning on line 5 in app/views/components/card/timeline_component.rb

View check run for this annotation

Codecov / codecov/patch

app/views/components/card/timeline_component.rb#L5

Added line #L5 was not covered by tests
include Phlex::Rails::Helpers::TimeAgoInWords

def view_template(&)
Expand All @@ -20,11 +21,40 @@
div(class: "flex-1 flex-col ml-5 md:ml-7 pb-4 border-b border-neutral-300 dark:border-neutral-700") do
div(class: "flex items-center justify-between") do
span(class: "text-b3 text-neutral-600") { t("time_ago", duration: time_ago_in_words(datetime)) }
span(class: "text-b3 text-neutral-800") { user_link } if user_link
span(class: "text-b3 text-neutral-800 dark:text-white max-h-6") { user_link } if user_link

Check warning on line 24 in app/views/components/card/timeline_component.rb

View check run for this annotation

Codecov / codecov/patch

app/views/components/card/timeline_component.rb#L24

Added line #L24 was not covered by tests
end

div(class: "flex flex-wrap w-full items-center justify-between", &)
end
end
end

def link_to_user(user)
link_to(profile_path(user.display_id), alt: user.display_handle, title: user.display_handle, class: "flex items-center") do
span(class: "w-6 h-6 inline-block mr-2 rounded") { helpers.avatar(48, "gravatar-#{user.id}", user) }
span { user.display_handle }
end
end

Check warning on line 37 in app/views/components/card/timeline_component.rb

View check run for this annotation

Codecov / codecov/patch

app/views/components/card/timeline_component.rb#L32-L37

Added lines #L32 - L37 were not covered by tests

def link_to_api_key(api_key_owner)
case api_key_owner
when OIDC::TrustedPublisher::GitHubAction
div(class: "flex items-center") do
span(class: "w-6 h-6 inline-block mr-2 rounded") do
image_tag "github_icon.png", width: 48, height: 48, theme: :light, alt: "GitHub", title: api_key_owner.name
end
span { "GitHub Actions" }
end
else
raise ArgumentError, "unknown api_key_owner type #{api_key_owner.class}"
end
end

Check warning on line 51 in app/views/components/card/timeline_component.rb

View check run for this annotation

Codecov / codecov/patch

app/views/components/card/timeline_component.rb#L39-L51

Added lines #L39 - L51 were not covered by tests

def link_to_pusher(version)
if version.pusher.present?
link_to_user(version.pusher)
elsif version.pusher_api_key&.owner.present?
link_to_api_key(version.pusher_api_key.owner)
end
end

Check warning on line 59 in app/views/components/card/timeline_component.rb

View check run for this annotation

Codecov / codecov/patch

app/views/components/card/timeline_component.rb#L53-L59

Added lines #L53 - L59 were not covered by tests
end
61 changes: 34 additions & 27 deletions app/views/dashboards/_subject.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,47 @@
current ||= :dashboard
%>

<div class="flex flex-wrap lg:flex-col items-start mb-6 lg:mb-10">
<%= avatar 328, "user_gravatar", theme: :dark, class: "h-24 w-24 lg:h-40 lg:w-40 rounded-lg object-cover mr-4" %>
<div class="mb-8 space-y-4">
<div class="flex flex-wrap lg:flex-col items-start mb-6 lg:mb-10">
<%= avatar 328, "user_gravatar", theme: :dark, class: "h-24 w-24 lg:h-40 lg:w-40 rounded-lg object-cover mr-4" %>

<div class="lg:w-full lg:mt-2">
<h2 class="font-bold text-h4"><%= user.display_handle %></h2>
<% if user.full_name.present? %>
<p class="text-neutral-500 text-b3"><%= user.full_name %></p>
<% end %>
<div class="lg:w-full lg:mt-2">
<h2 class="font-bold text-h4"><%= user.display_handle %></h2>
<% if user.full_name.present? %>
<p class="text-neutral-500 text-b3"><%= user.full_name %></p>
<% end %>
</div>
</div>
</div>

<% if user.public_email? || user == current_user %>
<div class="flex items-center mb-4 text-b3 lg:text-b2">
<%= icon_tag("mail", color: :primary, class: "h-6 w-6 text-orange mr-3") %>
<p class="text-neutral-800 dark:text-white"><%=
mail_to(user.email, encode: "hex")
%></p>
</div>
<% end %>
<% if user.public_email? || user == current_user %>
<div class="flex items-center mb-4 text-b3 lg:text-b2">
<%= icon_tag("mail", color: :primary, class: "h-6 w-6 text-orange mr-3") %>
<p class="text-neutral-800 dark:text-white"><%=
mail_to(user.email, encode: "hex")
%></p>
</div>
<% end %>

<% if user.twitter_username.present? %>
<div class="flex items-center mb-4 text-b3 lg:text-b2">
<%= icon_tag("x-twitter", color: :primary, class: "w-6 text-orange mr-3") %>
<p class="text-neutral-800 dark:text-white"><%=
link_to(
twitter_username(user),
twitter_url(user)
)
%></p>
</div>
<% end %>
<% if user.twitter_username.present? %>
<div class="flex items-center mb-4 text-b3 lg:text-b2">
<%= icon_tag("x-twitter", color: :primary, class: "w-6 text-orange mr-3") %>
<p class="text-neutral-800 dark:text-white"><%=
link_to(
twitter_username(user),
twitter_url(user)
)
%></p>
</div>
<% end %>
</div>

<hr class="hidden lg:block lg:mb-6 border-neutral-400 dark:border-neutral-600" />

<%= render Subject::NavComponent.new(current:) do |nav| %>
<%= nav.link t("layouts.application.header.dashboard"), dashboard_path, name: :dashboard, icon: "space-dashboard" %>
<%= nav.link t("dashboards.show.my_subscriptions"), subscriptions_path, name: :subscriptions, icon: "notifications" %>
<% if current_user.memberships.any? %>
<%= nav.link t("dashboards.show.organizations"), organizations_path, name: :organizations, icon: "organizations" %>
<% end %>
<%= nav.link t("layouts.application.header.settings"), edit_settings_path, name: :settings, icon: "settings" %>
<% end %>
20 changes: 8 additions & 12 deletions app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% @title = t('.title') %>

<% content_for :subject do %>
<% render "dashboards/subject", user: current_user %>
<%= render "dashboards/subject", user: current_user, current: :dashboard %>
<% end %>

<!-- Main Content -->
Expand All @@ -27,14 +27,7 @@
<%= c.scrollable do %>
<%= render Card::TimelineComponent.new do |t| %>
<% @latest_updates.each do |version| %>
<%
pusher_link = if version.pusher.present?
link_to_user(version.pusher)
elsif version.pusher_api_key&.owner.present?
link_to_pusher(version.pusher_api_key.owner)
end
%>
<%= t.timeline_item(version.authored_at, pusher_link) do %>
<%= t.timeline_item(version.authored_at, t.link_to_pusher(version)) do %>
<div class="flex text-b1 text-neutral-800 dark:text-white"><%= link_to version.rubygem.name, rubygem_path(version.rubygem.slug) %></div>
<%= version_number(version) %>
<% end %>
Expand Down Expand Up @@ -101,9 +94,12 @@
<% end %>
<%= c.divided_list do %>
<% current_user.memberships.preload(:organization).each do |membership| %>
<%= c.list_item_to("#") do %>
<div class="flex justify-between">
<p class="text-neutral-800 dark:text-white"><%= membership.organization.name %></p>
<%= c.list_item_to(organization_path(membership.organization)) do %>
<div class="flex flex-row w-full justify-between items-center">
<div class="flex flex-col">
<p class="text-neutral-800 dark:text-white"><%= membership.organization.name %></p>
<p class="text-b3 text-neutral-600"><%= membership.organization.handle %></p>
</div>
<p class="text-neutral-500 capitalize"><%= membership.role %></p>
</div>
<% end %>
Expand Down
Loading
Loading