1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

rubocop api controllers

TODO:
 - events controller
 - availabilies controller
 - members controller
 - plans controller
This commit is contained in:
Sylvain 2019-01-16 16:28:25 +01:00
parent c6cf86fa5c
commit 0cd841da33
35 changed files with 353 additions and 233 deletions

View File

@ -1,5 +1,5 @@
Metrics/LineLength:
Max: 140
Max: 130
Metrics/MethodLength:
Max: 30
Metrics/CyclomaticComplexity:

View File

@ -80,17 +80,14 @@ class API::AuthProvidersController < API::ApiController
if params['auth_provider']['providable_type'] == DatabaseProvider.name
params.require(:auth_provider).permit(:name, :providable_type)
elsif params['auth_provider']['providable_type'] == OAuth2Provider.name
params.require(:auth_provider).permit(:name, :providable_type,
providable_attributes: [:id, :base_url, :token_endpoint, :authorization_endpoint,
:logout_endpoint, :profile_url, :client_id, :client_secret,
o_auth2_mappings_attributes: [:id, :local_model, :local_field,
:api_field, :api_endpoint,
:api_data_type, :_destroy,
transformation: [:type,
:format,
:true_value,
:false_value,
mapping: %i[from to]]]])
params.require(:auth_provider)
.permit(:name, :providable_type,
providable_attributes: [:id, :base_url, :token_endpoint, :authorization_endpoint, :logout_endpoint,
:profile_url, :client_id, :client_secret,
o_auth2_mappings_attributes: [:id, :local_model, :local_field, :api_field,
:api_endpoint, :api_data_type, :_destroy,
transformation: [:type, :format, :true_value,
:false_value, mapping: %i[from to]]]])
end
end
end

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type Category
# Categories are used to classify Events
class API::CategoriesController < API::ApiController
before_action :authenticate_user!, except: [:index]
before_action :set_category, only: [:show, :update, :destroy]
before_action :set_category, only: %i[show update destroy]
def index
@categories = Category.all
end
def show
end
def show; end
def create
authorize Category
@ -39,11 +42,12 @@ class API::CategoriesController < API::ApiController
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name)
end
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name)
end
end

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type Component
# Components are used in Projects
class API::ComponentsController < API::ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_component, only: [:show, :update, :destroy]
before_action :authenticate_user!, except: %i[index show]
before_action :set_component, only: %i[show update destroy]
def index
@components = Component.all
end
def show
end
def show; end
def create
authorize Component
@ -35,11 +38,12 @@ class API::ComponentsController < API::ApiController
end
private
def set_component
@component = Component.find(params[:id])
end
def component_params
params.require(:component).permit(:name)
end
def set_component
@component = Component.find(params[:id])
end
def component_params
params.require(:component).permit(:name)
end
end

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type Coupon
# Coupons are used in payments
class API::CouponsController < API::ApiController
before_action :authenticate_user!
before_action :set_coupon, only: [:show, :update, :destroy]
before_action :set_coupon, only: %i[show update destroy]
def index
@coupons = Coupon.all
end
def show
end
def show; end
def create
authorize Coupon
@ -22,18 +25,18 @@ class API::CouponsController < API::ApiController
def validate
@coupon = Coupon.find_by(code: params[:code])
if @coupon.nil?
render json: {status: 'rejected'}, status: :not_found
render json: { status: 'rejected' }, status: :not_found
else
if !current_user.admin?
_user_id = current_user.id
else
_user_id = params[:user_id]
end
_user_id = if !current_user.admin?
current_user.id
else
params[:user_id]
end
amount = params[:amount].to_f * 100.0
status = @coupon.status(_user_id, amount)
if status != 'active'
render json: {status: status}, status: :unprocessable_entity
render json: { status: status }, status: :unprocessable_entity
else
render :validate, status: :ok, location: @coupon
end
@ -62,18 +65,17 @@ class API::CouponsController < API::ApiController
authorize Coupon
@coupon = Coupon.find_by(code: params[:coupon_code])
if @coupon.nil?
render json: {error: "no coupon with code #{params[:coupon_code]}"}, status: :not_found
else
if @coupon.send_to(params[:user_id])
render :show, status: :ok, location: @coupon
else
render json: @coupon.errors, status: :unprocessable_entity
end
end
if @coupon.nil?
render json: { error: "no coupon with code #{params[:coupon_code]}" }, status: :not_found
elsif @coupon.send_to(params[:user_id])
render :show, status: :ok, location: @coupon
else
render json: @coupon.errors, status: :unprocessable_entity
end
end
private
def set_coupon
@coupon = Coupon.find(params[:id])
end
@ -85,7 +87,8 @@ class API::CouponsController < API::ApiController
@parameters = params
@parameters[:coupon][:amount_off] = @parameters[:coupon][:amount_off].to_f * 100.0 if @parameters[:coupon][:amount_off]
@parameters = @parameters.require(:coupon).permit(:name, :code, :percent_off, :amount_off, :validity_per_user, :valid_until, :max_usages, :active)
@parameters = @parameters.require(:coupon).permit(:name, :code, :percent_off, :amount_off, :validity_per_user, :valid_until,
:max_usages, :active)
end
end

View File

@ -1,14 +1,18 @@
# frozen_string_literal: true
# API Controller for resources of type Credit
# Credits are used to give free reservations to users
class API::CreditsController < API::ApiController
before_action :authenticate_user!
before_action :set_credit, only: [:show, :update, :destroy]
before_action :set_credit, only: %i[show update destroy]
def index
authorize Credit
if params
@credits = Credit.includes(:creditable).where(params.permit(:creditable_type))
else
@credits = Credit.includes(:creditable).all
end
@credits = if params
Credit.includes(:creditable).where(params.permit(:creditable_type))
else
Credit.includes(:creditable).all
end
end
def create
@ -37,11 +41,12 @@ class API::CreditsController < API::ApiController
end
private
def set_credit
@credit = Credit.find(params[:id])
end
def credit_params
params.require(:credit).permit!
end
def set_credit
@credit = Credit.find(params[:id])
end
def credit_params
params.require(:credit).permit!
end
end

View File

@ -1,10 +1,10 @@
class API::CustomAssetsController < API::ApiController
before_action :authenticate_user!, only: [:index, :update, :create, :destroy]
before_action :set_custom_asset, only: [:show, :update, :destroy]
# frozen_string_literal: true
def index
#TODO GET /api/custom_assets/
end
# API Controller for resources of type CustomAsset
# CustomAssets are used in settings
class API::CustomAssetsController < API::ApiController
before_action :authenticate_user!, only: %i[index update create destroy]
before_action :set_custom_asset, only: %i[show update destroy]
# PUT /api/custom_assets/1/
def update
@ -28,14 +28,10 @@ class API::CustomAssetsController < API::ApiController
end
# GET /api/custom_assets/1/
def show
end
def destroy
#TODO DELETE /api/custom_assets/1/
end
def show; end
private
def set_custom_asset
@custom_asset = CustomAsset.find_by(name: params[:id])
end
@ -45,4 +41,4 @@ class API::CustomAssetsController < API::ApiController
params.required(:custom_asset).permit(:name, custom_asset_file_attributes: [:attachment])
end
end
end

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type EventTheme
# EventTheme are used to classify Events
class API::EventThemesController < API::ApiController
before_action :authenticate_user!, except: [:index]
before_action :set_event_theme, only: [:show, :update, :destroy]
before_action :set_event_theme, only: %i[show update destroy]
def index
@event_themes = EventTheme.all
end
def show
end
def show; end
def create
authorize EventTheme
@ -39,6 +42,7 @@ class API::EventThemesController < API::ApiController
end
private
def set_event_theme
@event_theme = EventTheme.find(params[:id])
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Export
# Export are used to download data tables in offline files
class API::ExportsController < API::ApiController
before_action :authenticate_user!
before_action :set_export, only: [:download]
@ -6,7 +10,9 @@ class API::ExportsController < API::ApiController
authorize @export
if FileTest.exist?(@export.file)
send_file File.join(Rails.root, @export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
send_file File.join(Rails.root, @export.file),
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment'
else
render text: I18n.t('errors.messages.export_not_found'), status: :not_found
end
@ -15,38 +21,39 @@ class API::ExportsController < API::ApiController
def status
authorize Export
export = Export.where({category: params[:category], export_type: params[:type], query: params[:query], key: params[:key]})
export = Export.where(category: params[:category], export_type: params[:type], query: params[:query], key: params[:key])
if params[:category] === 'users'
if params[:category] == 'users'
case params[:type]
when 'subscriptions'
export = export.where('created_at > ?', Subscription.maximum('updated_at'))
when 'reservations'
export = export.where('created_at > ?', Reservation.maximum('updated_at'))
when 'members'
export = export.where('created_at > ?', User.with_role(:member).maximum('updated_at'))
else
raise ArgumentError, "Unknown export users/#{params[:type]}"
when 'subscriptions'
export = export.where('created_at > ?', Subscription.maximum('updated_at'))
when 'reservations'
export = export.where('created_at > ?', Reservation.maximum('updated_at'))
when 'members'
export = export.where('created_at > ?', User.with_role(:member).maximum('updated_at'))
else
raise ArgumentError, "Unknown export users/#{params[:type]}"
end
elsif params[:category] === 'availabilities'
elsif params[:category] == 'availabilities'
case params[:type]
when 'index'
export = export.where('created_at > ?', Availability.maximum('updated_at'))
else
raise ArgumentError, "Unknown type availabilities/#{params[:type]}"
when 'index'
export = export.where('created_at > ?', Availability.maximum('updated_at'))
else
raise ArgumentError, "Unknown type availabilities/#{params[:type]}"
end
end
export = export.last
if export.nil? || !FileTest.exist?(export.file)
render json: {exists: false, id: nil}, status: :ok
render json: { exists: false, id: nil }, status: :ok
else
render json: {exists: true, id: export.id}, status: :ok
render json: { exists: true, id: export.id }, status: :ok
end
end
private
def set_export
@export = Export.find(params[:id])
end
end
end

View File

@ -1,16 +1,19 @@
# frozen_string_literal: true
# API Controller to wrap social networks public feeds
class API::FeedsController < API::ApiController
respond_to :json
def twitter_timelines
if params
limit = params[:limit]
else
limit = 3
end
limit = if params
params[:limit]
else
3
end
from_account = Setting.find_by(name: 'twitter_name').try(:value) || ENV['TWITTER_NAME']
begin
@tweet_news = Feed.twitter.user_timeline(from_account, {count: limit})
@tweet_news = Feed.twitter.user_timeline(from_account, count: limit)
rescue Twitter::Error::BadRequest => e
STDERR.puts "[WARNING] Unable to retrieve the twitter feed, please check your ENV configuration. Details: #{e.message}"
render status: :no_content

View File

@ -1,12 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type Group
# Groups are used for categorizing Users
class API::GroupsController < API::ApiController
before_action :authenticate_user!, except: :index
def index
if current_user and current_user.admin?
@groups = Group.all
else
@groups = Group.where.not(slug: 'admins')
end
@groups = if current_user&.admin?
Group.all
else
Group.where.not(slug: 'admins')
end
end
@ -39,7 +43,7 @@ class API::GroupsController < API::ApiController
private
def group_params
params.require(:group).permit(:name, :disabled)
end
def group_params
params.require(:group).permit(:name, :disabled)
end
end

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true
# API Controller for resources of type Licence
# Licenses are used in Projects
class API::LicencesController < API::ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_licence, only: [:show, :update, :destroy]
before_action :authenticate_user!, except: %i[index show]
before_action :set_licence, only: %i[show update destroy]
def index
@licences = Licence.all
end
def show
end
def show; end
def create
authorize Licence
@ -35,11 +38,12 @@ class API::LicencesController < API::ApiController
end
private
def set_licence
@licence = Licence.find(params[:id])
end
def licence_params
params.require(:licence).permit(:name, :description)
end
def set_licence
@licence = Licence.find(params[:id])
end
def licence_params
params.require(:licence).permit(:name, :description)
end
end

View File

@ -1,15 +1,18 @@
# frozen_string_literal: true
# API Controller for resources of type Machine
class API::MachinesController < API::ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_machine, only: [:update, :destroy]
before_action :authenticate_user!, except: %i[index show]
before_action :set_machine, only: %i[update destroy]
respond_to :json
def index
sort_by = Setting.find_by(name: 'machines_sort_by').value || 'default'
if sort_by === 'default'
@machines = Machine.includes(:machine_image, :plans)
else
@machines = Machine.includes(:machine_image, :plans).order(sort_by)
end
@machines = if sort_by == 'default'
Machine.includes(:machine_image, :plans)
else
Machine.includes(:machine_image, :plans).order(sort_by)
end
end
def show
@ -42,22 +45,14 @@ class API::MachinesController < API::ApiController
end
private
def set_machine
@machine = Machine.find(params[:id])
end
def machine_params
params.require(:machine).permit(:name, :description, :spec, :disabled, :plan_ids, plan_ids: [], machine_image_attributes: [:attachment],
machine_files_attributes: [:id, :attachment, :_destroy])
end
def set_machine
@machine = Machine.find(params[:id])
end
def is_reserved(start_at, reservations)
is_reserved = false
reservations.each do |r|
r.slots.each do |s|
is_reserved = true if s.start_at == start_at
end
end
is_reserved
end
def machine_params
params.require(:machine).permit(:name, :description, :spec, :disabled, :plan_ids,
plan_ids: [], machine_image_attributes: [:attachment],
machine_files_attributes: %i[id attachment _destroy])
end
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Notification
# Notifications are scoped by user
class API::NotificationsController < API::ApiController
include NotifyWith::NotificationsApi
before_action :authenticate_user!
@ -12,8 +16,8 @@ class API::NotificationsController < API::ApiController
break unless delete_obsoletes(@notifications)
end
@totals = {
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
}
render :index
end
@ -25,17 +29,19 @@ class API::NotificationsController < API::ApiController
break unless delete_obsoletes(@notifications)
end
@totals = {
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
}
render :index
end
def polling
@notifications = current_user.notifications.where('is_read = false AND created_at >= :date', date: params[:last_poll]).order('created_at DESC')
@notifications = current_user.notifications
.where('is_read = false AND created_at >= :date', date: params[:last_poll])
.order('created_at DESC')
@totals = {
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
total: current_user.notifications.count,
unread: current_user.notifications.where(is_read: false).count
}
render :index
end
@ -45,7 +51,7 @@ class API::NotificationsController < API::ApiController
def delete_obsoletes(notifications)
cleaned = false
notifications.each do |n|
if !Module.const_get(n.attached_object_type) or !n.attached_object
if !Module.const_get(n.attached_object_type) || !n.attached_object
n.destroy!
cleaned = true
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type OpenAPI::Client
# OpenAPI::Clients are used to allow access to the public API
class API::OpenAPIClientsController < API::ApiController
before_action :authenticate_user!
@ -5,7 +9,7 @@ class API::OpenAPIClientsController < API::ApiController
authorize OpenAPI::Client
@clients = OpenAPI::Client.order(:created_at)
end
# add authorization
def create
@client = OpenAPI::Client.new(client_params)
authorize @client
@ -40,7 +44,8 @@ class API::OpenAPIClientsController < API::ApiController
end
private
def client_params
params.require(:open_api_client).permit(:name)
end
def client_params
params.require(:open_api_client).permit(:name)
end
end

View File

@ -1,11 +1,13 @@
# frozen_string_literal: true
# API Controller for resources of type Openlab::Projects
# Openlab::Projects are Projects shared between different instances
class API::OpenlabProjectsController < API::ApiController
PROJECTS = Openlab::Projects.new
def index
begin
render json: PROJECTS.search(params[:q], page: params[:page], per_page: params[:per_page]).response.body
rescue StandardError
render json: { errors: ['service unavailable'] }
end
render json: PROJECTS.search(params[:q], page: params[:page], per_page: params[:per_page]).response.body
rescue StandardError
render json: { errors: ['service unavailable'] }
end
end

View File

@ -1,6 +1,10 @@
# frozen_string_literal: true
# API Controller for resources of type PriceCategory
# PriceCategories are used in Events
class API::PriceCategoriesController < API::ApiController
before_action :authenticate_user!, only: [:update, :show, :create, :destroy]
before_action :set_price_category, only: [:show, :update, :destroy]
before_action :authenticate_user!, only: %i[update show create destroy]
before_action :set_price_category, only: %i[show update destroy]
def index
@price_categories = PriceCategory.all
@ -15,8 +19,7 @@ class API::PriceCategoriesController < API::ApiController
end
end
def show
end
def show; end
def create
authorize PriceCategory
@ -38,6 +41,7 @@ class API::PriceCategoriesController < API::ApiController
end
private
def set_price_category
@price_category = PriceCategory.find(params[:id])
end
@ -45,4 +49,4 @@ class API::PriceCategoriesController < API::ApiController
def price_category_params
params.require(:price_category).permit(:name, :conditions)
end
end
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Price
# Prices are used in reservations (Machine, Space)
class API::PricesController < API::ApiController
before_action :authenticate_user!
@ -6,29 +10,26 @@ class API::PricesController < API::ApiController
@prices = Price.all
if params[:priceable_type]
@prices = @prices.where(priceable_type: params[:priceable_type])
if params[:priceable_id]
@prices = @prices.where(priceable_id: params[:priceable_id])
end
@prices = @prices.where(priceable_id: params[:priceable_id]) if params[:priceable_id]
end
if params[:plan_id]
if params[:plan_id] =~ /no|nil|null|undefined/i
plan_id = nil
else
plan_id = params[:plan_id]
end
plan_id = if params[:plan_id] =~ /no|nil|null|undefined/i
nil
else
params[:plan_id]
end
@prices = @prices.where(plan_id: plan_id)
end
if params[:group_id]
@prices = @prices.where(group_id: params[:group_id])
end
@prices = @prices.where(group_id: params[:group_id]) if params[:group_id]
end
def update
authorize Price
@price = Price.find(params[:id])
_price_params = price_params
_price_params[:amount] = _price_params[:amount] * 100
if @price.update(_price_params)
price_parameters = price_params
price_parameters[:amount] = price_parameters[:amount] * 100
if @price.update(price_parameters)
render status: :ok
else
render status: :unprocessable_entity
@ -36,15 +37,22 @@ class API::PricesController < API::ApiController
end
def compute
_price_params = compute_price_params
price_parameters = compute_price_params
# user
_user = User.find(_price_params[:user_id])
user = User.find(price_parameters[:user_id])
# reservable
if _price_params[:reservable_id].nil?
if price_parameters[:reservable_id].nil?
@amount = {elements: nil, total: 0, before_coupon: 0}
else
_reservable = _price_params[:reservable_type].constantize.find(_price_params[:reservable_id])
@amount = Price.compute(current_user.admin?, _user, _reservable, _price_params[:slots_attributes] || [], _price_params[:plan_id], _price_params[:nb_reserve_places], _price_params[:tickets_attributes], coupon_params[:coupon_code])
reservable = price_parameters[:reservable_type].constantize.find(price_parameters[:reservable_id])
@amount = Price.compute(current_user.admin?,
user,
reservable,
price_parameters[:slots_attributes] || [],
price_parameters[:plan_id],
price_parameters[:nb_reserve_places],
price_parameters[:tickets_attributes],
coupon_params[:coupon_code])
end
@ -56,14 +64,15 @@ class API::PricesController < API::ApiController
end
private
def price_params
params.require(:price).permit(:amount)
end
def compute_price_params
params.require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places,
tickets_attributes: [:event_price_category_id, :booked],
slots_attributes: [:id, :start_at, :end_at, :availability_id, :offered])
tickets_attributes: %i[event_price_category_id booked],
slots_attributes: %i[id start_at end_at availability_id offered])
end
def coupon_params

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
# API Controller for managing Plans prices
class API::PricingController < API::ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :authenticate_user!, except: %i[index show]
def index
@group_pricing = Group.includes(:plans, :trainings_pricings)
@ -10,14 +13,14 @@ class API::PricingController < API::ApiController
if params[:training].present?
training = Training.find params[:training]
params[:group_pricing].each do |group_id, amount|
if training
group = Group.includes(:plans).find(group_id)
if group
training_pricing = group.trainings_pricings.find_or_initialize_by(training_id: training.id)
training_pricing.amount = amount * 100
training_pricing.save
end
end
next unless training
group = Group.includes(:plans).find(group_id)
next unless group
training_pricing = group.trainings_pricings.find_or_initialize_by(training_id: training.id)
training_pricing.amount = amount * 100
training_pricing.save
end
end
head 200

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Project
class API::ProjectsController < API::ApiController
before_action :authenticate_user!, except: %i[index show last_published search]
before_action :set_project, only: %i[update destroy]

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Reservation
# Reservations are used for Training, Machine, Space and Event
class API::ReservationsController < API::ApiController
before_action :authenticate_user!
before_action :set_reservation, only: %i[show update]
@ -46,6 +50,7 @@ class API::ReservationsController < API::ApiController
end
private
def set_reservation
@reservation = Reservation.find(params[:id])
end

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Setting
class API::SettingsController < API::ApiController
before_action :authenticate_user!, only: :update

View File

@ -1,6 +1,10 @@
# frozen_string_literal: true
# API Controller for resources of type Slot
# Slots are used to cut Availabilities into reservable slots of ApplicationHelper::SLOT_DURATION minutes
class API::SlotsController < API::ApiController
before_action :authenticate_user!
before_action :set_slot, only: [:update, :cancel]
before_action :set_slot, only: %i[update cancel]
respond_to :json
def update
@ -15,10 +19,11 @@ class API::SlotsController < API::ApiController
def cancel
authorize @slot
@slot.update_attributes(:canceled_at => DateTime.now)
@slot.update_attributes(canceled_at: DateTime.now)
end
private
def set_slot
@slot = Slot.find(params[:id])
end

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
# API Controller for resources of type Space
class API::SpacesController < API::ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :authenticate_user!, except: %i[index show]
respond_to :json
def index
@ -38,12 +41,14 @@ class API::SpacesController < API::ApiController
end
private
def get_space
Space.friendly.find(params[:id])
end
def space_params
params.require(:space).permit(:name, :description, :characteristics, :default_places, :disabled, space_image_attributes: [:attachment],
space_files_attributes: [:id, :attachment, :_destroy])
end
def get_space
Space.friendly.find(params[:id])
end
def space_params
params.require(:space).permit(:name, :description, :characteristics, :default_places, :disabled,
space_image_attributes: [:attachment],
space_files_attributes: %i[id attachment _destroy])
end
end

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Space
class API::StatisticsController < API::ApiController
before_action :authenticate_user!
@ -6,7 +9,7 @@ class API::StatisticsController < API::ApiController
@statistics = StatisticIndex.all
end
%w(account event machine project subscription training user space).each do |path|
%w[account event machine project subscription training user space].each do |path|
class_eval %{
def #{path}
authorize :statistic, :#{path}?
@ -27,38 +30,50 @@ class API::StatisticsController < API::ApiController
# return result
render json: results
end
}, __FILE__, __LINE__ - 20
end
%w[account event machine project subscription training user space].each do |path|
class_eval %{
def export_#{path}
authorize :statistic, :export_#{path}?
export = Export.where({category:'statistics', export_type: '#{path}', query: params[:body], key: params[:type_key]}).last
export = Export.where(category:'statistics', export_type: '#{path}', query: params[:body], key: params[:type_key]).last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new({category:'statistics', export_type: '#{path}', user: current_user, query: params[:body], key: params[:type_key]})
@export = Export.new(category:'statistics',
export_type: '#{path}',
user: current_user,
query: params[:body],
key: params[:type_key])
if @export.save
render json: {export_id: @export.id}, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
send_file File.join(Rails.root, export.file),
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment'
end
end
}
}, __FILE__, __LINE__ - 22
end
def export_global
authorize :statistic, :export_global?
export = Export.where({category:'statistics', export_type: 'global', query: params[:body]}).last
export = Export.where(category: 'statistics', export_type: 'global', query: params[:body]).last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new({category:'statistics', export_type: 'global', user: current_user, query: params[:body]})
@export = Export.new(category: 'statistics', export_type: 'global', user: current_user, query: params[:body])
if @export.save
render json: {export_id: @export.id}, status: :ok
render json: { export_id: @export.id }, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
send_file File.join(Rails.root, export.file),
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment'
end
end
@ -68,5 +83,4 @@ class API::StatisticsController < API::ApiController
results = Elasticsearch::Model.client.scroll scroll: params[:scroll], scroll_id: params[:scrollId]
render json: results
end
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Stylesheet
# Stylesheets are used to customize the appearance of fab-manager
class API::StylesheetsController < API::ApiController
caches_page :show # magic happens here
@ -5,7 +9,7 @@ class API::StylesheetsController < API::ApiController
@stylesheet = Stylesheet.find(params[:id])
respond_to do |format|
format.html # regular ERB template
format.css { render :text => @stylesheet.contents, :content_type => 'text/css' }
format.css { render text: @stylesheet.contents, content_type: 'text/css' }
end
end
end
end

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Subscription
class API::SubscriptionsController < API::ApiController
include FablabConfiguration
@ -64,7 +67,7 @@ class API::SubscriptionsController < API::ApiController
params.require(:subscription).permit(:expired_at)
end
# TODO refactor subscriptions logic and move this in model/validator
# TODO, refactor subscriptions logic and move this in model/validator
def valid_card_token?(token)
Stripe::Token.retrieve(token)
rescue Stripe::InvalidRequestError => e

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Tag
# Tags are used to restrict access to Availabilities
class API::TagsController < API::ApiController
before_action :authenticate_user!, except: %i[index show]
@ -35,6 +39,7 @@ class API::TagsController < API::ApiController
end
private
def set_tag
@tag = Tag.find(params[:id])
end

View File

@ -1,3 +1,7 @@
# frozen_string_literal: true
# API Controller for resources of type Theme
# Themes are used in Projects
class API::ThemesController < API::ApiController
before_action :authenticate_user!, except: %i[index show]
before_action :set_theme, only: %i[show update destroy]

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Training
class API::TrainingsController < API::ApiController
include ApplicationHelper
@ -7,14 +10,12 @@ class API::TrainingsController < API::ApiController
def index
@requested_attributes = params[:requested_attributes]
@trainings = policy_scope(Training)
if params[:public_page]
@trainings = @trainings.where(public_page: true)
end
@trainings = @trainings.where(public_page: true) if params[:public_page]
if attribute_requested?(@requested_attributes, 'availabilities')
@trainings = @trainings.includes(availabilities: [slots: [reservation: [user: %i[profile trainings]]]])
.order('availabilities.start_at DESC')
end
return unless attribute_requested?(@requested_attributes, 'availabilities')
@trainings = @trainings.includes(availabilities: [slots: [reservation: [user: %i[profile trainings]]]])
.order('availabilities.start_at DESC')
end
def show

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for managing Training prices
class API::TrainingsPricingsController < API::ApiController
before_action :authenticate_user!
@ -8,9 +11,9 @@ class API::TrainingsPricingsController < API::ApiController
def update
if current_user.admin?
@trainings_pricing = TrainingsPricing.find(params[:id])
_trainings_pricing_params = trainings_pricing_params
_trainings_pricing_params[:amount] = _trainings_pricing_params[:amount] * 100
if @trainings_pricing.update(_trainings_pricing_params)
trainings_pricing_parameters = trainings_pricing_params
trainings_pricing_parameters[:amount] = trainings_pricing_parameters[:amount] * 100
if @trainings_pricing.update(trainings_pricing_parameters)
render status: :ok
else
render status: :unprocessable_entity

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for managing front-end translations
class API::TranslationsController < API::ApiController
before_action :set_locale

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Users with role :partner
class API::UsersController < API::ApiController
before_action :authenticate_user!

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller to get the fab-manager version
class API::VersionController < API::ApiController
before_action :authenticate_user!

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# API Controller for resources of type Wallet
class API::WalletController < API::ApiController
before_action :authenticate_user!
@ -19,9 +22,7 @@ class API::WalletController < API::ApiController
service = WalletService.new(user: current_user, wallet: @wallet)
transaction = service.credit(credit_params[:amount].to_f)
if transaction
if credit_params[:avoir]
service.create_avoir(transaction, credit_params[:avoir_date], credit_params[:avoir_description])
end
service.create_avoir(transaction, credit_params[:avoir_date], credit_params[:avoir_description]) if credit_params[:avoir]
render :show
else
head 422