Skip to content

Commit

Permalink
Add links
Browse files Browse the repository at this point in the history
  • Loading branch information
duleorlovic committed Jan 8, 2020
1 parent 3161528 commit 9d1df5f
Show file tree
Hide file tree
Showing 60 changed files with 752 additions and 142 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rails credentials:edit -e development
Icons

```
gnome-open https://localhost:3000/fontello-demo.html
gnome-open http://localhost:3001/fontello-demo.html
# when you want to update you can
fontello open
# select new icons
Expand Down
47 changes: 47 additions & 0 deletions app/controllers/admin/links_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Admin::LinksController < Admin::BaseController
before_action :_set_link, except: %i[index search new create]

def index
@datatable = LinksDatatable.new view_context
end

def search
render json: LinksDatatable.new(view_context)
end

def show; end

def new
@link = Link.new
render partial: 'form', layout: false
end

def edit
render partial: 'form', layout: false
end

def create
@link = Link.new

update_and_render_or_redirect_in_js @link, _link_params, ->(id) { link_path(id) }
end

def update
update_and_render_or_redirect_in_js @link, _link_params, link_path(@link)
end

def destroy
@link.destroy!
redirect_to admin_links_path, notice: helpers.t_notice('successfully_deleted', Link)
end

def _set_link
@link = Link.find(params[:id])
end

def _link_params
params.require(:link).permit(
*Link::FIELDS
)
end
end
12 changes: 9 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ def update_and_render_or_redirect_in_js(item, item_params, redirect_path_or_proc
else
redirect_path_or_proc
end
render js: %(
window.location.assign('#{redirect_path}');
)
if redirect_path.present?
render js: %(
window.location.assign('#{redirect_path}');
)
else
render js: %(
window.location.reload();
)
end
else
flash.now[:alert] = item.errors.full_messages.to_sentence
render js: %(
Expand Down
58 changes: 58 additions & 0 deletions app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class LinksController < ApplicationUserController
before_action :_set_link, except: %i[index search new create]

def index
@datatable = LinksDatatable.new view_context
end

def search
render json: LinksDatatable.new(view_context)
end

def show; end

def new
if params[:happening_id].present?
linkable_id = params[:happening_id]
linkable_type = 'Happening'
elsif params[:club_id].present?
linkable_id = params[:club_id]
linkable_type = 'Club'
else
raise "do_not_know_linkable_type params=#{params}"
end
@link = Link.new(
linkable_id: linkable_id,
linkable_type: linkable_type,
)
render partial: 'form', layout: false
end

def edit
render partial: 'form', layout: false
end

def create
@link = Link.new
update_and_render_or_redirect_in_js @link, _link_params, nil
end

def update
update_and_render_or_redirect_in_js @link, _link_params, link_path(@link)
end

def destroy
@link.destroy!
redirect_to links_path, notice: helpers.t_notice('successfully_deleted', Link)
end

def _set_link
@link = Link.find(params[:id])
end

def _link_params
params.require(:link).permit(
*Link::FIELDS
)
end
end
44 changes: 44 additions & 0 deletions app/controllers/venues_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class VenuesController < ApplicationUserController
before_action :_set_venue, except: %i[index new create]

def index
@venues = Venue.all
end

def show; end

def new
@venue = Venue.new
render partial: 'form', layout: false
end

def edit
render partial: 'form', layout: false
end

# JS
def create
@venue = Venue.new
update_and_render_or_redirect_in_js @venue, _venue_params, ->(id) { venue_path(id) }
end

# JS
def update
update_and_render_or_redirect_in_js @venue, _venue_params, venue_path(@venue)
end

def destroy
@venue.destroy!
redirect_to venues_path, notice: helpers.t_notice('successfully_deleted', Venue)
end

def _set_venue
@venue = Venue.find params[:id]
end

def _venue_params
params.require(:venue).permit(
*Venue::FIELDS
)
end
end
4 changes: 2 additions & 2 deletions app/datatables/clubs_datatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ class ClubsDatatable < BaseDatatable
def columns
{
'clubs.id': { hide: true },
'clubs.name': {},
'venues.name': { title: Venue.model_name.human },
'clubs.website': {},
'activities.name': { title: Activity.model_name.human(count: 2), search: false, order: false },
'clubs.name': {},
}
end

Expand All @@ -18,10 +18,10 @@ def rows(filtered)
end
[
club.id,
link,
club.venue.name,
club.website,
club.activities.map(&:name).to_sentence,
link,
]
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/datatables/happenings_for_activity_names_datatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class HappeningsForActivityNamesDatatable < BaseDatatable
def columns
{
'happenings.id': { hide: true },
'happenings.name': {},
'happenings.start_date': {},
'venues.name': { title: Venue.model_name.human },
'clubs.name': { title: Club.model_name.human },
'clubs.id': { hide: true },
'happenings.name': {},
}
end

Expand Down Expand Up @@ -42,11 +42,11 @@ def rows(filtered)
end
[
happening.id,
@view.link_to(happening.name, @view.happening_path(happening)),
happening.start_date,
happening.venue.name,
link_to_club,
happening.club.id,
@view.link_to(happening.name, @view.happening_path(happening)),
]
end
end
Expand Down
29 changes: 29 additions & 0 deletions app/datatables/links_datatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class LinksDatatable < TrkDatatables::ActiveRecord
def columns
{
'links.id': {},
'links.linkable_type': {},
'links.linkable_id': {},
'links.kind': {},
'links.url': {},
}
end

def all_items
# you can use @view.params
Link.all
end

def rows(filtered)
# you can use @view.link_to and other helpers
filtered.map do |link|
[
@view.link_to(link.id, link),
link.linkable_type,
link.linkable_id,
link.kind,
link.url,
]
end
end
end
1 change: 1 addition & 0 deletions app/models/club.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Club < ApplicationRecord
has_many :active_users, -> { where club_users: { position: ClubUser::ACTIVE_POSITIONS } }, through: :club_users, source: :user
has_many :activity_clubs, dependent: :destroy
has_many :activities, through: :activity_clubs
has_many :links, as: :linkable

validates :name, :venue, presence: true
end
1 change: 1 addition & 0 deletions app/models/happening.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Happening < ApplicationRecord
belongs_to :club

has_many :discipline_happenings, dependent: :destroy
has_many :links, as: :linkable

validates :name, :start_date, :end_date, presence: true
validate :_end_date_greater_or_equal_start_date
Expand Down
14 changes: 14 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Link < ApplicationRecord
FIELDS = %i[linkable_id linkable_type kind url].freeze
KINDS = %i[
club_website
club_facebook_page
happening_website
happening_results
happening_photos
].each_with_object({}) { |k, o| o[k] = k.to_s }
enum kind: KINDS

belongs_to :linkable, polymorphic: true
validates :url, presence: true
end
15 changes: 6 additions & 9 deletions app/views/admin/happenings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
<%= button_tag_open_modal edit_recurrence_admin_happening_path(@happening), title: t('edit_item_name', item_name: Happening.human_attribute_name(:recurrence)) %>
<% end %>
<% end %>
<%= link_to @happening.club.name, admin_club_path(@happening.club) %>
</div>
<div class='col-md'>
<%= link_to @happening.venue.name, admin_venue_path(@happening.venue) %>
<div class='map map--300px'
data-controller='google-map'
data-google-map-latitude='<%= @happening.venue.latitude %>'
data-google-map-longitude='<%= @happening.venue.longitude %>'
data-action='google-map-callback@window->google-map#initMap'
>
</div>
<%= detail_view_list do %>
<%= detail_view_one Happening.human_attribute_name(:club), link_to(@happening.club.name, admin_club_path(@happening.club)) %>
<%= detail_view_one Happening.human_attribute_name(:venue), link_to(@happening.venue.name, admin_venue_path(@happening.venue)) %>
<% end %>
<% @happening.links.each do |link| %>
<% end %>
</div>
</div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions app/views/admin/links/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id='remote-form'>
<%= bootstrap_form_with model: @link, layout: :horizontal do |f| %>
<%= f.text_field :linkable_id %>
<%= f.text_field :kind %>
<%= f.text_field :url %>
<div class='remote-form__actions'>
<% unless @link.new_record? %>
<%= link_to admin_link_path(@link), method: :delete, 'data-confirm': t_are_you_sure_to_remove_item_name(@link.linkable_id), title: t_crud('delete', Link), class: 'btn btn-outline-danger' do %>
<%= t('delete') %>
<i class="demo-icon icon-trash-empty" aria-hidden="true"></i>
<% end %>
<% end %>
<%= f.submit class: 'btn btn-primary' %>
</div>
<% end %>
</div>
11 changes: 11 additions & 0 deletions app/views/admin/links/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%
breadcrumb Link.model_name.human(count: 2) => nil
%>

<div class='float-left'>
<%= button_tag_open_modal new_admin_link_path do %>
<i class="demo-icon icon-plus" aria-hidden="true"></i>
<%= t_crud('add_new', Link) %>
<% end %>
</div>
<%= @datatable.render_html search_admin_links_path(format: :json) %>
9 changes: 9 additions & 0 deletions app/views/admin/links/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%
breadcrumb Link.model_name.human(count: 2) => links_path, @link.linkable_id => nil
%>
<div class='card'>
<div class='card-body'>
<%= button_tag_open_modal edit_link_path(@link), title: t_crud('edit', Link), pull_right: true %>
<%= detail_view_list @link, *Link::FIELDS %>
</div>
</div>
18 changes: 18 additions & 0 deletions app/views/clubs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
<%= activity_club.activity.name %>
</span>
<% end %>
<h2><%= Link.model_name.human(count: 2) %></h2>
<% @club.links.each do |link| %>
<%= link.kind %>
<%= link_to link.url, link.url %>
<% if current_user&.superadmin? %>
<%= link_to link_path(link), method: :delete, 'data-confirm': t_are_you_sure_to_remove_item_name(link.url), title: t_crud('delete', Link) do %>
<i class="demo-icon icon-trash-empty text-danger" aria-hidden="true"></i>
<% end %>
<% end %>
<% end %>
<% if current_user&.superadmin? %>
<div class=''>
<%= button_tag_open_modal new_club_link_path(@club) do %>
<i class="demo-icon icon-plus" aria-hidden="true"></i>
<%= t_crud('add_new', Link) %>
<% end %>
</div>
<% end %>
</div>
</div>
<div class='col-md'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<% if activity.self_and_used_in_activities.length > 1 %>
<div class='dropdown-item'>
<%= link_to activity.name, add_path, class: 'dropdown-item--inline' %>
<%= link_to '∈', add_related_path, class: 'btn btn-small btn-info dropdown-item--inline', title: t('activity_name_and_used_in_activities_name', activity_name: activity.name, activities_name: activity.self_and_used_in_activities.map(&:name).to_sentence) %>
<%= link_to t('and_related'), add_related_path, class: 'btn btn-small btn-primary dropdown-item--inline', title: t('activity_name_and_used_in_activities_name', activity_name: activity.name, activities_name: activity.self_and_used_in_activities.map(&:name).to_sentence) %>
</div>
<% else %>
<%= link_to activity.name, add_path, class:'dropdown-item' %>
Expand Down
Loading

0 comments on commit 9d1df5f

Please sign in to comment.