Skip to content

Commit

Permalink
Create CommunityLinks.
Browse files Browse the repository at this point in the history
Links must be https.  Add validate_url ruby gem to validate the urls.

Add not null on link site and url.

Add relevant FK.
  • Loading branch information
openbrian committed May 30, 2024
1 parent 74dc025 commit c7b51f1
Show file tree
Hide file tree
Showing 20 changed files with 492 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ gem "rails_param"
gem "rinku", ">= 2.0.6", :require => "rails_rinku"
gem "strong_migrations"
gem "validates_email_format_of", ">= 1.5.1"
gem "validate_url"

# Native OSM extensions
gem "quad_tile", "~> 1.0.1"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ GEM
unf_ext (0.0.9.1)
unicode-display_width (2.5.0)
uri (0.13.0)
validate_url (1.0.15)
activemodel (>= 3.0.0)
public_suffix
validates_email_format_of (1.8.2)
i18n (>= 0.8.0)
simpleidn
Expand Down Expand Up @@ -700,6 +703,7 @@ DEPENDENCIES
terser
turbo-rails
unicode-display_width
validate_url
validates_email_format_of (>= 1.5.1)
vendorer
webmock
Expand Down
4 changes: 3 additions & 1 deletion app/abilities/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def initialize(user)
if Settings.status != "database_offline"
can [:index, :feed, :show], Changeset
can :index, ChangesetComment
can [:index, :show], Community
can [:index], CommunityLink
can [:confirm, :confirm_resend, :confirm_email], :confirmation
can [:index, :rss, :show, :comments], DiaryEntry
can [:index, :show], Community
can [:index], Note
can [:new, :create, :edit, :update], :password
can [:index, :show], Redaction
Expand Down Expand Up @@ -47,6 +48,7 @@ def initialize(user)
can [:new, :create, :reply, :show, :inbox, :outbox, :muted, :mark, :unmute, :destroy], Message
can [:create, :new], Community
can [:edit, :update], Community, { :organizer_id => user.id }
can [:edit, :create, :destroy, :new, :update], CommunityLink, { :community => { :organizer_id => user.id } }
can [:close, :reopen], Note
can [:show, :edit, :update], :preference
can [:edit, :update], :profile
Expand Down
6 changes: 0 additions & 6 deletions app/assets/stylesheets/communities.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@
label {
font-weight: bold;
}
ul {
display: inline-block;
}
ul > li {
display: inline-block;
}
}
61 changes: 61 additions & 0 deletions app/controllers/community_links_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class CommunityLinksController < ApplicationController
layout "site"
before_action :authorize_web

before_action :set_link, :only => [:destroy, :edit, :update]

load_and_authorize_resource :except => [:create, :new]
authorize_resource

def index
@community = Community.friendly.find(params[:community_id])
@links = @community.community_links
end

def new
return "missing parameter community_id" unless params.key?(:community_id)

@community = Community.friendly.find(params[:community_id])
@title = t ".title"
@link = CommunityLink.new
@link.community_id = params[:community_id]
end

def edit; end

def create
@community = Community.friendly.find(params[:community_id])
@link = @community.community_links.build(link_params)
if @link.save
response.set_header("link_id", @link.id) # for testing
redirect_to @link.community, :notice => t(".success")
else
render "new"
end
end

def update
if @link.update(link_params)
redirect_to @link.community, :notice => t(".success")
else
flash.now[:alert] = t(".failure")
render :edit
end
end

def destroy
community_id = @link.community_id
@link.delete
redirect_to community_path(community_id)
end

private

def set_link
@link = CommunityLink.find(params[:id])
end

def link_params
params.require(:community_link).permit(:community_id, :site, :url)
end
end
1 change: 1 addition & 0 deletions app/models/community.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Community < ApplicationRecord
friendly_id :name, :use => :slugged

belongs_to :organizer, :class_name => "User"
has_many :community_links

validates :name, :presence => true, :length => 1..255, :characters => true
validates :description, :presence => true, :length => 1..1023, :characters => true
Expand Down
25 changes: 25 additions & 0 deletions app/models/community_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
#
# Table name: community_links
#
# id :bigint(8) not null, primary key
# community_id :bigint(8) not null
# site :string not null
# url :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_community_links_on_community_id (community_id)
#
# Foreign Keys
#
# fk_rails_... (community_id => communities.id)
#

class CommunityLink < ApplicationRecord
belongs_to :community
validates :site, :presence => true, :length => 1..255, :characters => true
validates :url, :presence => true, :length => 1..255, :url => { :schemes => ["https"] }
end
26 changes: 26 additions & 0 deletions app/views/communities/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,36 @@
<p>
<%= auto_link @community.description %>
</p>
<div class="d-flex">
<label>
<% if current_user == @community.organizer %>
<%= link_to t(".links"), community_community_links_path(@community) %>
<% else %>
<%= t(".links") %>
<% end %>
</label>
<ul class="ps-2 breadcrumb">
<% @community.community_links.each do |link| %>
<li class="breadcrumb-item">
<a href="<%= link.url %>"><%= link.site %></a>
</li>
<% end %>
</ul>
</div>
<div>
<label><%= t(".organizer") %></label>
<%= link_to @community.organizer.display_name, user_path(@community.organizer) %>
</div>
<div>
<label><%= t(".links") %></label>
<ul>
<% @community.community_links.each do |link| %>
<li>
<a href="<%= link.url %>"><%= link.site %></a>
</li>
<% end %>
</ul>
</div>
</div>
</div>
<div class="row">
Expand Down
5 changes: 5 additions & 0 deletions app/views/community_links/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= bootstrap_form_for [@community, @link] do |form| %>
<%= form.text_field :site %>
<%= form.text_field :url %>
<%= form.primary %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/community_links/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= t(".edit_community_link") %></h1>

<%= render "form", :link => @link %>
27 changes: 27 additions & 0 deletions app/views/community_links/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<% content_for :heading do %>
<h1><%= t(".title") %></h1>
<nav class="secondary-actions">
<ul class='clearfix'>
<li><%= link_to image_tag("new.png", :class => "small_icon", :border => 0) + t(".new"), new_community_community_link_path(@community) %></li>
</ul>
</nav>
<% end %>
<% if !@links.empty? %>
<table class="table table-borderless table-striped">
<tbody>
<% @links.each do |link| %>
<tr>
<td>
<% link.url.slice! "https://" %> <%# prevent XSS %>
<%= link_to link.site, "https://#{link.url}" %>
</td>
<td>
<%= link_to t(".edit"), edit_community_link_path(link) %>
<%= link_to t(".delete"), community_link_path(link), :method => :delete %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
5 changes: 5 additions & 0 deletions app/views/community_links/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% content_for :heading do %>
<h1><%= @title %></h1>
<% end %>
<%= render "form", :link => @link %>
20 changes: 19 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ en:
email_address_not_routable: is not routable
display_name_is_user_n: can't be user_n unless n is your user id
models:
url: "is not a valid secure URL"
user_mute:
attributes:
subject:
Expand Down Expand Up @@ -518,6 +519,22 @@ en:
title_particular: "OpenStreetMap changeset #%{changeset_id} discussion"
timeout:
sorry: "Sorry, the list of changeset comments you requested took too long to retrieve."
community_links:
create:
success: "Community Link was successfully created."
edit:
edit_community_link: "Edit Community Link"
index:
delete: "Delete"
edit: "Edit"
new: "New"
title: "Community Links"
new:
all: "All"
title: "New Community Link"
update:
failure: "The community link could not be updated."
success: "The community link was successfully updated."
communities:
create:
success: "Community was successfully created."
Expand All @@ -526,15 +543,16 @@ en:
index:
all: "All Communities"
communities_organized: "Communities Organized"
longitude: "Longitude"
new: "New"
new_title: "Create a new community"
sorted_by: "Sorted by name"
title: "Communities"
new:
title: "New Community"
show:
edit: "Edit"
header_title: "Community"
links: "Links"
organizer: "Organizer"
recent_changes: "Recent Changes"
report: "Report"
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,10 @@
end

# communities
resources :communities
resources :communities do
resources :community_links, :only => [:create, :index, :new]
end
resources :community_links, :only => [:destroy, :edit, :update]

# errors
match "/400", :to => "errors#bad_request", :via => :all
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240525143545_create_community_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCommunityLinks < ActiveRecord::Migration[7.0]
def change
create_table :community_links do |t|
t.references :community, :null => false, :foreign_key => true, :index => true
t.string :site, :null => false
t.string :url, :null => false

t.timestamps
end
end
end
Loading

0 comments on commit c7b51f1

Please sign in to comment.