Skip to content

Commit 2b20225

Browse files
committed
Create CommunityLinks.
Links must be https. Add validate_url ruby gem to validate the urls. Add not null on link site and url. Add relevant FK.
1 parent e30bbe4 commit 2b20225

File tree

19 files changed

+467
-8
lines changed

19 files changed

+467
-8
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ gem "rails_param"
6767
gem "rinku", ">= 2.0.6", :require => "rails_rinku"
6868
gem "strong_migrations", "< 2.0.0"
6969
gem "validates_email_format_of", ">= 1.5.1"
70+
gem "validate_url"
7071

7172
# Native OSM extensions
7273
gem "quad_tile", "~> 1.0.1"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ GEM
583583
concurrent-ruby (~> 1.0)
584584
unicode-display_width (2.5.0)
585585
uri (0.13.0)
586+
validate_url (1.0.15)
587+
activemodel (>= 3.0.0)
588+
public_suffix
586589
validates_email_format_of (1.8.2)
587590
i18n (>= 0.8.0)
588591
simpleidn
@@ -703,6 +706,7 @@ DEPENDENCIES
703706
terser
704707
turbo-rails
705708
unicode-display_width
709+
validate_url
706710
validates_email_format_of (>= 1.5.1)
707711
vendorer
708712
webmock

app/abilities/ability.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(user)
1818
can [:index, :feed, :show], Changeset
1919
can :index, ChangesetComment
2020
can [:index, :show], Community
21+
can [:index], CommunityLink
2122
can [:confirm, :confirm_resend, :confirm_email], :confirmation
2223
can [:index, :rss, :show], DiaryEntry
2324
can :index, DiaryComment
@@ -50,6 +51,7 @@ def initialize(user)
5051
can [:new, :create, :reply, :show, :inbox, :outbox, :muted, :mark, :unmute, :destroy], Message
5152
can [:create, :new], Community
5253
can [:edit, :update], Community, { :organizer_id => user.id }
54+
can [:edit, :create, :destroy, :new, :update], CommunityLink, { :community => { :organizer_id => user.id } }
5355
can [:close, :reopen], Note
5456
can [:show, :edit, :update], :preference
5557
can [:edit, :update], :profile

app/assets/stylesheets/communities.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,4 @@
99
label {
1010
font-weight: bold;
1111
}
12-
ul {
13-
display: inline-block;
14-
}
15-
ul > li {
16-
display: inline-block;
17-
}
1812
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class CommunityLinksController < ApplicationController
2+
layout "site"
3+
before_action :authorize_web
4+
5+
before_action :set_link, :only => [:destroy, :edit, :update]
6+
7+
load_and_authorize_resource :except => [:create, :new]
8+
authorize_resource
9+
10+
def index
11+
@community = Community.friendly.find(params[:community_id])
12+
@links = @community.community_links
13+
end
14+
15+
def new
16+
return "missing parameter community_id" unless params.key?(:community_id)
17+
18+
@community = Community.friendly.find(params[:community_id])
19+
@title = t ".title"
20+
@link = CommunityLink.new
21+
@link.community_id = params[:community_id]
22+
end
23+
24+
def edit; end
25+
26+
def create
27+
@community = Community.friendly.find(params[:community_id])
28+
@link = @community.community_links.build(link_params)
29+
if @link.save
30+
response.set_header("link_id", @link.id) # for testing
31+
redirect_to @link.community, :notice => t(".success")
32+
else
33+
render "new"
34+
end
35+
end
36+
37+
def update
38+
if @link.update(link_params)
39+
redirect_to @link.community, :notice => t(".success")
40+
else
41+
flash.now[:alert] = t(".failure")
42+
render :edit
43+
end
44+
end
45+
46+
def destroy
47+
community_id = @link.community_id
48+
@link.delete
49+
redirect_to community_path(community_id)
50+
end
51+
52+
private
53+
54+
def set_link
55+
@link = CommunityLink.find(params[:id])
56+
end
57+
58+
def link_params
59+
params.require(:community_link).permit(:community_id, :text, :url)
60+
end
61+
end

app/models/community.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Community < ApplicationRecord
3535
friendly_id :name, :use => :slugged
3636

3737
belongs_to :organizer, :class_name => "User"
38+
has_many :community_links
3839

3940
validates :name, :presence => true, :length => 1..255, :characters => true
4041
validates :description, :presence => true, :length => 1..1023, :characters => true

app/models/community_link.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# == Schema Information
2+
#
3+
# Table name: community_links
4+
#
5+
# id :bigint(8) not null, primary key
6+
# community_id :bigint(8) not null
7+
# text :string not null
8+
# url :string not null
9+
# created_at :datetime not null
10+
# updated_at :datetime not null
11+
#
12+
# Indexes
13+
#
14+
# index_community_links_on_community_id (community_id)
15+
#
16+
# Foreign Keys
17+
#
18+
# fk_rails_... (community_id => communities.id)
19+
#
20+
21+
class CommunityLink < ApplicationRecord
22+
belongs_to :community
23+
validates :text, :presence => true, :length => 1..255, :characters => true
24+
validates :url, :presence => true, :length => 1..255, :url => { :schemes => ["https"] }
25+
end

app/views/communities/show.html.erb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
<p>
3030
<%= auto_link @community.description %>
3131
</p>
32+
<div class="d-flex">
33+
<label>
34+
<% if current_user == @community.organizer %>
35+
<%= link_to t(".links"), community_community_links_path(@community) %>
36+
<% else %>
37+
<%= t(".links") %>
38+
<% end %>
39+
</label>
40+
<ul class="ps-2 breadcrumb">
41+
<% @community.community_links.each do |link| %>
42+
<li class="breadcrumb-item">
43+
<a href="<%= link.url %>"><%= link.site %></a>
44+
</li>
45+
<% end %>
46+
</ul>
47+
</div>
3248
<div>
3349
<label><%= t(".organizer") %></label>
3450
<%= link_to @community.organizer.display_name, user_path(@community.organizer) %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<%= bootstrap_form_for [@community, @link] do |form| %>
2+
<%= form.text_field :text %>
3+
<%= form.text_field :url %>
4+
<%= form.primary %>
5+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1><%= t(".edit_community_link") %></h1>
2+
3+
<%= render "form", :link => @link %>

0 commit comments

Comments
 (0)