Skip to content

Commit

Permalink
Ver 2.1.1 (#26)
Browse files Browse the repository at this point in the history
* Utenti caricati asincroni per la creazione dei ticket
* vari fix
  • Loading branch information
MdreW authored May 4, 2021
1 parent a33a24f commit 2ab379d
Show file tree
Hide file tree
Showing 25 changed files with 341 additions and 216 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.1]
### changed
- fixed fact destroy
- fixed happening destroy
- fixed happening creations

## [2.1.0]
### Added
- Users' ticket manage for editor
Expand Down
24 changes: 12 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GEM
ast (2.4.2)
bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.7.3)
bootsnap (1.7.4)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
Expand Down Expand Up @@ -118,7 +118,7 @@ GEM
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
deep_merge (1.2.1)
devise (4.7.3)
devise (4.8.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
Expand All @@ -138,10 +138,10 @@ GEM
dry-equalizer (0.3.0)
dry-inflector (0.2.0)
dry-initializer (3.0.4)
dry-logic (1.1.0)
dry-logic (1.2.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.5, >= 0.5)
dry-schema (1.6.1)
dry-schema (1.6.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.8, >= 0.8.3)
dry-core (~> 0.5, >= 0.5)
Expand Down Expand Up @@ -170,7 +170,7 @@ GEM
ffi (1.15.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
hamlit (2.14.6)
hamlit (2.15.0)
temple (>= 0.8.2)
thor
tilt
Expand Down Expand Up @@ -199,7 +199,7 @@ GEM
method_source (1.0.0)
mini_magick (4.11.0)
mini_mime (1.0.3)
mini_portile2 (2.5.0)
mini_portile2 (2.5.1)
minitest (5.14.4)
msgpack (1.4.2)
net-scp (3.0.0)
Expand All @@ -210,9 +210,9 @@ GEM
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (4.2.0)
pagy (4.5.1)
parallel (1.20.1)
parser (3.0.1.0)
parser (3.0.1.1)
ast (~> 2.4.1)
pg (1.2.3)
public_suffix (4.0.6)
Expand Down Expand Up @@ -261,7 +261,7 @@ GEM
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.2.5)
rubocop (1.12.1)
rubocop (1.13.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
Expand All @@ -270,8 +270,8 @@ GEM
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.1)
parser (>= 2.7.1.5)
rubocop-ast (1.5.0)
parser (>= 3.0.1.1)
rubocop-rails (2.9.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
Expand Down Expand Up @@ -329,7 +329,7 @@ GEM
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
webpacker (5.2.1)
webpacker (5.3.0)
activesupport (>= 5.2)
rack-proxy (>= 0.6.1)
railties (>= 5.2)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ class ApplicationController < ActionController::Base

before_action :authenticate_user!

rescue_from ActiveRecord::RecordNotFound do
record_not_found!
end
#rescue_from ActiveRecord::RecordNotFound do
# record_not_found!
#end

private

# Render 404 page and stop the work
# @return [nil]
def record_not_found!
render partial: 'errors/404', status: 404 && return
render partial: 'errors/404', status: 404 and return
end

# Render 401 page and stop the work
# @return [nil]
def access_denied!
render 'errors/401', status: :unauthorized
render 'errors/401', status: :unauthorized and return
end
end
6 changes: 3 additions & 3 deletions app/controllers/editor/facts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create
@fact = Fact.new(fact_params)
if @fact.save
@status = { success: 'Evento creato' }
render action: :show
redirect_to editor_fact_path(@fact)
else
@status = { error: 'ERRORE - Evento non creato. Verifica gli errori.' }
render action: :new
Expand All @@ -51,9 +51,9 @@ def update

# DELETE /editor/facts/:id
def destroy
if @editor_fact.destroy
if @fact.destroy
@status = { success: 'Evento eliminato' }
render action: :index
redirect_to editor_facts_path
else
@status = { error: 'ERRORE - Evento non eliminato. Verifica gli errori.' }
render action: :edit
Expand Down
58 changes: 42 additions & 16 deletions app/controllers/editor/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@
# This controller manage {Ticket} model for editors
class Editor::TicketsController < Editor::ApplicationController
require 'csv'
before_action :set_fact
before_action :set_happening
before_action :check_happening, only: %i[create update]
#before_action :set_fact_ids
#before_action :set_happening
before_action :set_ticket, only: %i[show edit update destroy]

# GET /editor/facts/:fact_id/happenings/:happening_id/tickets
# GET /editor/tickets
def index
@text = ["users.username ilike '%:string%'", { string: params[:text] }] if params[:text].present?
end

# GET /editor/tickets/list
def list_by_happening
@happening = Happening.find_by(id: params[:id], fact_id: fact_ids)
@text = ["users.username ilike :string", { string: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
@pagy, @tickets = pagy(
@happening.tickets.includes(:user).where(@text),
items: 6
)
render action: :list
end

def list_by_user
@user = User.find(params[:id])
@text = ["facts.title ilike :string", { string: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
@pagy, @tickets = pagy(
@happening.tickets.joins(:user).where(@text),
@user.tickets.includes(happening: [:fact]).where(@text).where(happening: {fact_id: fact_ids}),
items: 6
)
render action: :list
end

# GET /editor/facts/:fact_id/happenings/:happening_id/tickets/export
Expand All @@ -28,7 +45,7 @@ def export

# GET /editor/facts/:fact_id/happenings/:happening_id/tickets/new
def new
@ticket = @happening.tickets.new
@ticket = Ticket.new(happening_id: happening_id(filter_params[:happening_id]))
@users = User.pluck :username, :id
end

Expand All @@ -39,11 +56,11 @@ def edit

# POST /editor/facts/:fact_id/happenings/:happening_id/tickets/
def create
@ticket = @happening.tickets.new(ticket_params)
@ticket = Ticket.new(ticket_params)
@ticket.by_editor = true
if @ticket.save
flash[:success] = 'Prenotazione salvata'
redirect_to editor_fact_happening_tickets_path(@fact, @happening)
redirect_to tickets_editor_fact_happening_path(@ticket.happening.fact, @ticket.happening)
else
@users = User.pluck :username, :id
@status = { error: 'Creatione prenotazione fallita' }
Expand All @@ -53,8 +70,7 @@ def create

# PATCH/PUT /editor/facts/:fact_id/happenings/:happening_id/tickets/:id
def update
@ticket.by_editor = true
if @ticket.update(ticket_params)
if @ticket.update(ticket_params.merge(by_editor: true))
flash[:success] = 'Prenotazione salvata'
render partial: 'ticket', locals: { ticket: @ticket, happening: @happening, fact: @fact }
else
Expand All @@ -72,24 +88,34 @@ def destroy
end

private

def check_happening
access_denied! unless fact_ids.include?(Happening.find(ticket_params[:happening_id]).fact_id)
end

# set @fact before any action
def set_fact
@fact = current_user.facts.find(params[:fact_id])
def fact_ids
current_user.facts.pluck(:id)
end

# Set @happening before any action
def set_happening
@happening = @fact.happenings.find(params[:happening_id])
def happening_id(value)
Happening.find_by(id: value, fact_id: fact_ids).id
end

# Set ticket when needed
def set_ticket
@ticket = @happening.tickets.find(params[:id])
@ticket = Ticket.includes(:happening).find(params[:id])
access_denied! unless fact_ids.include?(@ticket.happening.fact_id)
end

# Filter params for set a ticket
def ticket_params
params.require(:ticket).permit(:happenings_id, :user_id, :seats)
params.require(:ticket).permit(:happening_id, :user_id, :seats)
end

# Filter params for search an {Happening}
def filter_params
params.fetch(:filter, {}).permit(:text, :user_id, :happening_id)
end
end
19 changes: 13 additions & 6 deletions app/controllers/editor/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ def index; end

# GET /editor/users/list
def list
options = {}
options[:admin] = true if filter_params[:admin].present?
options[:editor] = true if filter_params[:editor].present?
@text = ['username ilike :text or email ilike :text', { text: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
@pagy, @users = pagy(User.where(@text))
@pagy, @users = pagy(User.where(@text).where(options))
end

# GET /editor/users/:id
Expand All @@ -18,10 +21,14 @@ def show; end
# GET /editor/users/:id/tickets
def tickets
@text = ['facts.title ilike :string', { string: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
@pagy, @tickets = pagy(
@user.tickets.joins(happening: [:fact]).where(happening: { fact: @current_user.facts }).where(@text),
items: 6
)
if current_user.admin?
@pagy, @tickets = pagy( @user.tickets.joins(happening: [:fact]).where(@text), items: 6)
else
@pagy, @tickets = pagy(
@user.tickets.joins(happening: [:fact]).where(happening: { fact: @current_user.facts }).where(@text),
items: 6
)
end
end

private
Expand All @@ -33,6 +40,6 @@ def set_user

# Filter params for search a {Fact}
def filter_params
params.fetch(:filter, {}).permit(:text)
params.fetch(:filter, {}).permit(:text, :admin, :editor)
end
end
10 changes: 9 additions & 1 deletion app/javascript/controllers/slim_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ export default class extends Controller {
new SlimSelect({ select: element })
}

user(element) {
this.ajax(element, '/editor/users/list.json?')
}

editor(element) {
this.ajax(element, '/admin/users/list?filter[editor]=1&')
}

get_data(element) {
[{text: element.getAttribute("data-text"), value: element.getAttribute("data-value")}]
}

ajax(element, url) {
new SlimSelect({
const slim = new SlimSelect({
select: element,
placeholder: 'Seleziona un utente',
searchingText: 'Sto cercando...',
Expand Down
2 changes: 1 addition & 1 deletion app/models/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Fact < ApplicationRecord
has_rich_text :body
has_one_attached :image
belongs_to :group
has_many :happenings
has_many :happenings, dependent: :destroy
has_many :tickets, through: :happenings
validates :title, presence: true
validates :start_on, presence: true
Expand Down
4 changes: 2 additions & 2 deletions app/models/happening.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# @return [Array] list of [Fact] with stop_on minor than Time.zone.today, ordered by start_on desc
class Happening < ApplicationRecord
belongs_to :fact, counter_cache: true
has_many :tickets
has_many :tickets, dependent: :destroy
validates :fact, presence: true
validates :start_at, presence: true
validates :start_sale_at, presence: true
Expand Down Expand Up @@ -74,7 +74,7 @@ def code
# save multiple copies of the elements
def add_repetitions
(1..repeat_for.to_i).each do |n|
next unless repeat_for.include?(start_at.+(n.days).wday.to_s)
next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

fact.happenings.create(
detail: detail,
Expand Down
2 changes: 1 addition & 1 deletion app/views/editor/facts/_fact_menu.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
= f.radio_button :type, 'history', selected: true, data: {action: "form#sendForm"}
%span= t('history', scope: 'site.happening.scope')
%a.panel-block{href: new_editor_fact_happening_path(@fact), data: {turbo_frame: 'happenings'}}= fas_icon 'plus', text: 'Aggiungi date'
%a.panel-block{href: editor_facts_path}= fas_icon 'chevron-left', text: 'Indietro'
%a.panel-block{href: editor_facts_path, data: {turbo_frame: 'yield'}}= fas_icon 'chevron-left', text: 'Indietro'

%figure.image= image_tag(fact.image.variant(resize_to_limit: [318,318]).processed)
3 changes: 3 additions & 0 deletions app/views/editor/facts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@
- else
%a.button.is-link{href: list_editor_facts_path, data: {turbo_frame: 'facts'}}= fas_icon 'chevron-left', text: 'Indietro'
= form.button fas_icon('save', text: 'Salva'), class: 'button is-success'
- if fact.persisted?
%br
.has-text-centered= link_to fas_icon('trash', text: 'Elimina evento'), editor_fact_path(fact), class: 'button is-warning', data: {confirm: 'Attenzione l`evento, le date e tutte le prenotazioni saranno eliminate, confermi?', method: :delete, turbo_frame: 'yield'}
5 changes: 4 additions & 1 deletion app/views/editor/happenings/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@

.has-text-centered
- if happening.persisted?
%a.button.is-link{href: editor_fact_happening_tickets_path(fact, happening), data: {turbo_frame: 'tickets'}}= fas_icon 'chevron-left', text: 'Indietro'
%a.button.is-link{href: tickets_editor_fact_happening_path(fact, happening), data: {turbo_frame: 'tickets'}}= fas_icon 'chevron-left', text: 'Indietro'
-else
%a.button.is-link{href: editor_fact_happenings_path(fact), data: {turbo_frame: 'happenings'}}= fas_icon 'chevron-left', text: 'Indietro'
= form.button fas_icon('save', text: 'Salva'), class: 'button is-success'
- if happening.persisted?
%br
.has-text-centered= link_to fas_icon('trash', text: 'Elimina data'), editor_fact_happening_path(happening.fact, happening), class: 'button is-warning', data: {confirm: 'Attenzione la data e tutte le prenotazioni saranno eliminate, confermi?', method: :delete}
Loading

0 comments on commit 2ab379d

Please sign in to comment.