diff --git a/app/assets/javascripts/cafe_works.js.coffee b/app/assets/javascripts/cafe_works.js.coffee index 1486edb13..6a6d1b996 100644 --- a/app/assets/javascripts/cafe_works.js.coffee +++ b/app/assets/javascripts/cafe_works.js.coffee @@ -1,5 +1,6 @@ jQuery -> - $('.datetimepicker').datetimepicker locale: 'sv', format: 'DD/MM/YYYY HH:mm' + $('.datetimepicker').datetimepicker + format: 'DD/MM/YYYY HH:mm' jQuery -> $('#date').datepicker() jQuery -> diff --git a/app/assets/javascripts/rents.js.coffee b/app/assets/javascripts/rents.js.coffee index 7d596747a..cdb2372ae 100644 --- a/app/assets/javascripts/rents.js.coffee +++ b/app/assets/javascripts/rents.js.coffee @@ -1,3 +1,8 @@ +jQuery -> + $('.datetimepicker1').datetimepicker + format: 'DD/MM/YYYY HH:mm' + $('.datetimepicker2').datetimepicker + format: 'DD/MM/YYYY HH:mm' loadCalendar = -> $('#bilkalender').fullCalendar events: '/bil', @@ -35,4 +40,4 @@ $('document').ready -> else $('.after_disclaimer').hide() if $('#rent_disclaimer').is(':checked') - $('.after_disclaimer').show() \ No newline at end of file + $('.after_disclaimer').show() diff --git a/app/controllers/admin/rents_controller.rb b/app/controllers/admin/rents_controller.rb index 07ad8a57a..80eefdb51 100644 --- a/app/controllers/admin/rents_controller.rb +++ b/app/controllers/admin/rents_controller.rb @@ -1,6 +1,5 @@ class Admin::RentsController < ApplicationController load_permissions_and_authorize_resource - before_action :set_rent, only: [:show, :update, :destroy, :preview] before_action :set_councils, only: [:new, :show] def main @@ -18,11 +17,10 @@ def preview def create @rent = Rent.new_with_status(rent_params, nil) flash[:notice] = 'Bokningen skapades' if @rent.save(validate: false) - respond_with @rent + redirect_to @rent end def new - @rent = Rent.new end def update @@ -37,22 +35,6 @@ def destroy private - def authenticate - flash[:error] = t('the_role.access_denied') - redirect_to(:back) unless (current_user) && (current_user.moderator?(:bil)) - rescue ActionController::RedirectBackError - redirect_to root_path - end - - # Makes sure that a rent is found, otherwise redirects to admin page - def set_rent - @rent = Rent.find_by_id(params[:id]) - if (@rent == nil) - flash[:notice] = 'Hittade ingen bilbokning med det ID:t.' - redirect_to(:admin_car) - end - end - # To set the councils def set_councils @councils = Council.all diff --git a/app/controllers/albums_controller.rb b/app/controllers/albums_controller.rb index ff572adef..4d1fca6c2 100644 --- a/app/controllers/albums_controller.rb +++ b/app/controllers/albums_controller.rb @@ -1,214 +1,88 @@ # encoding:UTF-8 class AlbumsController < ApplicationController load_permissions_and_authorize_resource + load_and_authorize_resource :image, through: :album before_action :set_edit - before_action :set_album, except: [:index,:new,:create,:settings] before_action :categories - + def index @albums = Album.order('start_date asc') - @albums_latest = Album.order('created_at desc LIMIT 4') - - if (params[:id] != nil) && (params[:datum] != "") - @id = params[:id] - @datum = Date.parse(params[:datum]) - if @datum < Date.today - if @kategorier.find_by_id(@id) - @searched = Album.where(:start_date => @datum..Date.today).all - else - @searched = Album.where(:category => @kategorier.find(@id).name, :start_date => @datum...Date.today).all - end - end - elsif (params[:id]) && (params[:datum] == "" ) - @id = params[:id] - @searched = Album.where(:category => @kategorier.find(@id).name) - end - + @albums_latest = Album.order('created_at desc LIMIT 4') end - + def edit - if(params[:commit]) && (current_user) && (current_user.moderator?(:galleri)) - if(params[:commit] == 'Markera alla') - @mark = true - elsif(params[:commit] == 'Ta bort markerade') && (params[:image_ids]) - params[:image_ids].each { |img_id| - img = Image.find_by_id(img_id) - if(img) - img.destroy - end - } - flash.now[:notice] = 'De markerade bilderna togs bort.' - elsif(params[:commit] == 'Byt kategori') && (params[:image_ids]) && (params[:image_category]) - params[:image_ids].each { |img_id| - img = Image.find_by_id(img_id) - if(img) - img.subcategory_id = params[:image_category] - img.save() - end - } - flash.now[:notice] = 'De markerade bilderna har nu kategorin: '+Subcategory.find_by_id(params[:image_category]).text - end - end - - end - - def settings - unless @kategori - @kategori = AlbumCategory.new - end - unless @kategorier - @kategorier = AlbumCategory.order('name desc') - end - - if (params[:commit] == "Spara ny kategori") && (params[:album_category][:name] != nil) - @kategori.update(name: params[:album_category][:name],text: params[:album_category][:text],visible: params[:album_category][:visible]) - if @kategori.save - flash.now[:notice] = 'Kategorin '+@kategori.name+' skapades till Bildgalleriet' - @kategorier = AlbumCategory.order('name desc') - @kategori = AlbumCategory.new - end - end - - if (params[:commit] == 'Spara kategori') && (params[:album_category][:id].nil? == false) - @kategorin = AlbumCategory.find(params[:album_category][:id]) - @kategorin.update(name: params[:album_category][:name],text: params[:album_category][:text],visible: params[:album_category][:visible]) - if @kategorin.save - @kategorier = AlbumCategory.unscoped.order('name desc') - flash.now[:notice] = 'Kategorin '+@kategorin.name+' uppdaterades till Bildgalleriet' - end - end - - if (params[:commit] == 'Ta bort kategori')&&(params[:album_category]) - @category = AlbumCategory.find_by_id(params[:album_category][:id]).destroy - @kategorier = AlbumCategory.all - flash.now[:notice] = 'Kategorin togs bort' - end - - unless @subcategory - @subcategory = Subcategory.new - end - unless @subcategories - @subcategories = Subcategory.order('text desc') - end - - if (params[:commit] == "Spara ny underkategori") && (params[:subcategory][:text] != nil) - @subcategory.update(text: params[:subcategory][:text]) - if @subcategory.save - flash.now[:notice] = 'Underkategorin '+@subcategory.text+' skapades till Bildgalleriet' - @subcategory = Subcategory.new() - - end - end - - if (params[:commit] == 'Spara underkategori') && (params[:subcategory][:id].nil? == false) - @subcategory = Subcategory.find(params[:subcategory][:id].nil? == false) - @subcategory.update(text: params[:subcategory][:text]) - if @subcategory.save - @subcategories = Subcategory.unscoped.order('text desc') - flash.now[:notice] = 'Underkategorin '+@subcategory.text+' uppdaterades till Bildgalleriet' - end - end - - if (params[:commit] == 'Ta bort underkategori')&&(params[:subcategory][:id].nil? == false) - @category = Subcategory.find(params[:subcategory][:id]).destroy - @subcategories = Subcategory.unscoped.order('text desc') - flash.now[:notice] = 'Kategorin togs bort' - end - end - - def show + + # TODO add Settings somewhere else + + def show if (@album.images) @images = @album.images.order('foto_file_name asc') else - @images = nil - end + @images = nil + end end - + def new - @album = Album.new end - + def create - @album = Album.new(album_params) - @album.update(author: current_user.profile) - respond_to do |format| - if @album.save - format.html { redirect_to @album, notice: 'Albumet skapades!' } - format.json { render :json => @album, :status => :created, :location => @album } - else - format.html { render action: "new" } - format.json { render json: @album.errors, status: :unprocessable_entity } - end - end + @album.author = current_user.profile + if @album.save + redirect_to @album, notice: 'Albumet skapades!' + else + render action: :new + end end - - def destroy + + def destroy @album.destroy - respond_to do |format| - format.html { redirect_to albums_url,notice: 'Albumet raderades.' } - format.json { head :no_content } - end + redirect_to albums_url, notice: 'Albumet raderades.' end + def destroy_images for image in @album.images image.destroy end - respond_to do |format| - format.html { redirect_to @album, notice: 'Bilderna tog borts!' } - format.json { render :json => @album, :location => @album } - end + redirect_to @album, notice: 'Bilderna tog borts!' end + def upload_images if (params[:fotos]) && (params[:subcategory_id]) - #===== The magic is here ;) - @count = 1 - @total = params[:fotos].count - params[:fotos].each { |foto| - flash[:notice] =("Laddar upp "+@count.to_s+"/"+@total.to_s) - @album.images.create(foto: foto,subcategory_id: params[:subcategory_id]) - @count = @count+1; - } - flash[:notice] =("Färdig! Laddat upp "+@total.to_s+" bilder.") - end + #===== The magic is here ;) + @count = 1 + @total = params[:fotos].count + params[:fotos].each do |foto| + flash[:notice] = %(Laddar upp #{@count}/#{@total}) + @album.images.create(foto: foto, subcategory_id: params[:subcategory_id]) + @count = @count + 1 + end + flash[:notice] = %(Färdig! Laddat upp #{@total} bilder.) + end end + def update - respond_to do |format| - if @album.update_attributes(album_params) - format.html { redirect_to edit_album_path(@album), :notice => 'Albumet uppdaterades!' } - format.json { head :no_content } - else - format.html { render :action => "edit" } - format.json { render :json => @album.errors, :status => :unprocessable_entity } - end + if @album.update(album_params) + redirect_to edit_album_path(@album), notice: 'Albumet uppdaterades!' + else + render action: :edit end - end -private - def authenticate - - redirect_to(:back) unless (current_user) && (current_user.moderator?(:galleri)) - - rescue ActionController::RedirectBackError - redirect_to root_path end + + private + def categories - @kategorier = AlbumCategory.order('name desc') - @subcategories = Subcategory.order('text desc') - end - def set_album - @album = Album.find(params[:id]) + @kategorier = AlbumCategory.order('name desc') + @subcategories = Subcategory.order('text desc') end + def set_edit - if (current_user) && (current_user.moderator?(:galleri)) - @edit = true - else - @edit = false - end - end - def image_params - params.fetch(:image,{}).permit(:album_id,:subcategory_id) + @edit = can? :manage, Album end + def album_params - params.fetch(:album,{}).permit(:title,:description,:author,:location,:public,:start_date,:end_date,:album_category_ids => [],:subcategory_ids => [],images_parameters: [:id, :foto]) + params.require(:album).permit(:title, :description, :author, :location, + :public, :start_date, :end_date, album_category_ids: [], + subcategory_ids: [], images_parameters: [:id, :foto]) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b06929fd4..9b443552b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,8 +6,11 @@ class ApplicationController < ActionController::Base before_action :set_locale rescue_from CanCan::AccessDenied do |ex| - flash[:error] = ex.message - render text: '', layout: true, status: :forbidden + if current_user.nil? + redirect_to :new_user_session, alert: ex.message + else + redirect_to :root, alert: ex.message + end end rescue_from ActiveRecord::RecordInvalid do |ex| diff --git a/app/controllers/councils_controller.rb b/app/controllers/councils_controller.rb index bcd4da06b..cf645ca1f 100644 --- a/app/controllers/councils_controller.rb +++ b/app/controllers/councils_controller.rb @@ -1,19 +1,14 @@ # encoding:UTF-8 class CouncilsController < ApplicationController load_permissions_and_authorize_resource find_by: :url - before_action :set_page, only: :show + load_and_authorize_resource :post, through: :council + before_action :set_page, only: [:show, :edit] before_action :set_councils def index end def show - if @page - @mainelements = @page.page_elements.where(visible: true, - sidebar: false).order(:displayIndex, :asc) - @sidebarelements = @page.page_elements.where(visible: true, - sidebar: true).order(:displayIndex, :asc) - end @poster = @council.posts end @@ -26,7 +21,6 @@ def edit def create if @council.save - @council.build_page!(council_id: @council.id) redirect_to edit_council_path(@council), notice: 'Utskott skapades, success.' else render action: 'new' diff --git a/app/controllers/election/candidates_controller.rb b/app/controllers/elections/candidates_controller.rb similarity index 74% rename from app/controllers/election/candidates_controller.rb rename to app/controllers/elections/candidates_controller.rb index 296955afc..f14310a19 100644 --- a/app/controllers/election/candidates_controller.rb +++ b/app/controllers/elections/candidates_controller.rb @@ -1,5 +1,5 @@ # encoding: UTF-8 -class Election::CandidatesController < ApplicationController +class Elections::CandidatesController < ApplicationController before_action :set_election load_permissions_and_authorize_resource respond_to :html @@ -25,7 +25,7 @@ def create @candidate.profile = current_user.profile if @candidate.save flash[:notice] = 'Kandidaturen skapades.' - redirect_to [:election, @candidate] + redirect_to @candidate else render action: :new end @@ -34,7 +34,7 @@ def create def update if @candidate.update(candidate_params) flash[:notice] = 'Kandidaturen uppdaterades' - redirect_to [:election, @candidate] + redirect_to @candidate else render action: :show end @@ -43,17 +43,10 @@ def update def destroy @candidate.destroy flash[:notice] = 'Kandidaturen raderades' - redirect_to election_candidates_path + redirect_to candidates_path end private - def set_candidate - @candidate = Candidate.find_by_id(params[:id]) - if !@candidate.owner?(current_user) - flash[:error] = 'Du har inte rättigheter för att se kandidaturen.' - redirect_to(elections_path) - end - end def set_election @election = Election.current diff --git a/app/controllers/election/nominations_controller.rb b/app/controllers/elections/nominations_controller.rb similarity index 90% rename from app/controllers/election/nominations_controller.rb rename to app/controllers/elections/nominations_controller.rb index 2b47dbc23..397640f8c 100644 --- a/app/controllers/election/nominations_controller.rb +++ b/app/controllers/elections/nominations_controller.rb @@ -1,5 +1,5 @@ # encoding: UTF-8 -class Election::NominationsController < ApplicationController +class Elections::NominationsController < ApplicationController before_action :set_election load_permissions_and_authorize_resource diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb index ed2787f71..fc6c47180 100644 --- a/app/controllers/faqs_controller.rb +++ b/app/controllers/faqs_controller.rb @@ -1,10 +1,7 @@ # encoding:UTF-8 class FaqsController < ApplicationController - - before_action :authenticate_editor, only: [:edit, :update, :destroy] + load_permissions_and_authorize_resource before_action :set_editor, only: [:new, :show, :edit, :index] - before_action :set_faq, only: [:show, :edit, :update, :destroy] - def index @faq = Faq.where.not(answer: '').where(category: 'main') @@ -17,7 +14,6 @@ def show end def new - @faq = Faq.new if params[:category].present? @faq.category = params[:category] end @@ -27,63 +23,36 @@ def edit end def destroy - @faq.destroy() - respond_to do |format| - format.html { redirect_to :faqs } - format.json { head :no_content } - end + @faq.destroy + redirect_to :faqs end def update - respond_to do |format| - if @faq.update(faq_params) - format.html { redirect_to @faq, notice: 'FAQ uppdaterades!' } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @faq.errors, status: :unprocessable_entity } - end + if @faq.update(faq_params) + redirect_to @faq, notice: 'FAQ uppdaterades!' + else + render action: 'edit' end end def create - @faq = Faq.new(faq_params) - if @faq.answer == nil + if @faq.answer.nil? @faq.answer = '' end - respond_to do |format| - if @faq.save - format.html { redirect_to @faq, notice: 'Frågan skapades!' } - format.json { render action: 'show', status: :created, location: @faq } - else - format.html { render action: 'new' } - format.json { render json: @faq.errors, status: :unprocessable_entity } - end + if @faq.save + redirect_to @faq, notice: 'Frågan skapades!' + else + render action: 'new' end end private - def set_faq - @faq = Faq.find(params[:id]) - end - - def authenticate_editor - if !(current_user) || !(current_user.moderator?(:faq)) - flash[:error] = "Funkar inte" - redirect_to :faq - end - end def set_editor - if (current_user) && (current_user.moderator?(:faq)) - @editor = true - else - @editor = false - end + @editor = can? :manage, Faq end def faq_params params.require(:faq).permit(:question, :answer, :category) end - end diff --git a/app/controllers/notices_controller.rb b/app/controllers/notices_controller.rb index f9b4f8754..b748aedec 100644 --- a/app/controllers/notices_controller.rb +++ b/app/controllers/notices_controller.rb @@ -1,92 +1,59 @@ class NoticesController < ApplicationController - before_action :authenticate, except: [:image] - before_action :set_notice, only: [:show, :edit, :update, :destroy,:hide,:display,:image] + load_permissions_and_authorize_resource def index - @notices = Notice.all @notice_grid = initialize_grid(@notices) end def show - end def new - @notice = Notice.new end def edit - end def create - @notice = Notice.new(notice_params) - respond_to do |format| - if @notice.save - format.html { redirect_to @notice, notice: %(#{t(:notice)} #{t(:success_create)}.)} - format.json { render action: 'show', status: :created, location: @notice } - else - format.html { render action: 'new' } - format.json { render json: @notice.errors, status: :unprocessable_entity } - end + if @notice.save + redirect_to @notice, notice: %(#{t(:notice)} #{t(:success_create)}.) + else + render action: :new end end def update - respond_to do |format| - if @notice.update(notice_params) - format.html { redirect_to @notice, notice: %(#{t(:notice)} #{t(:success_update)}.) } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @notice.errors, status: :unprocessable_entity } - end + if @notice.update(notice_params) + redirect_to @notice, notice: %(#{t(:notice)} #{t(:success_update)}.) + else + render action: :edit end end def destroy @notice.destroy - respond_to do |format| - format.html { redirect_to notices_url } - format.json { head :no_content } - end - end - - # Requested to hide the current Notice - # /d.wessman - def hide - @notice.display(false) - end - - # Requested to display the current Notice - # /d.wessman - def display - @notice.display(true) + redirect_to notices_url end - # Action to show profile picture only already authenticated def image if @notice.image? - if(params[:style] == "original" || params[:style] == "large" || params[:style] == "small") - send_file(@notice.image.path(params[:style]), filename:@notice.image_file_name, type: "image/jpg",disposition: 'inline',x_sendfile: true) + if params[:style] == 'original' || params[:style] == 'large' || params[:style] == 'small' + send_file(@notice.image.path(params[:style]), + filename: @notice.image_file_name, type: 'image/jpg', + disposition: 'inline', x_sendfile: true) else - send_file(@notice.image.path(:large), filename:@notice.image_file_name, type: "image/jpg",disposition: 'inline',x_sendfile: true) + send_file(@notice.image.path(:large), + filename: @notice.image_file_name, type: 'image/jpg', + disposition: 'inline', x_sendfile: true) end end end private - def authenticate - redirect_to root_path unless current_user && (current_user.moderator?(:notiser)) - end - # Use callbacks to share common setup or constraints between actions. - def set_notice - @notice = Notice.find_by_id(params[:id]) - end - - # Never trust parameters from the scary internet, only allow the white list through. - def notice_params - params.require(:notice).permit(:title, :description, :public, :d_publish, :d_remove, :sort,:image) - end + def notice_params + params.require(:notice).permit(:title, :description, + :public, :d_publish, :d_remove, + :sort, :image) + end end diff --git a/app/controllers/old_gallery_controller.rb b/app/controllers/old_gallery_controller.rb index 303d52624..d1723cad3 100644 --- a/app/controllers/old_gallery_controller.rb +++ b/app/controllers/old_gallery_controller.rb @@ -1,5 +1,5 @@ class OldGalleryController < ApplicationController - authorize_resource :class => false + authorize_resource class: false def index end end diff --git a/app/controllers/page_elements_controller.rb b/app/controllers/page_elements_controller.rb index c2362485f..844a0ff11 100644 --- a/app/controllers/page_elements_controller.rb +++ b/app/controllers/page_elements_controller.rb @@ -1,92 +1,49 @@ # encoding:UTF-8 class PageElementsController < ApplicationController - before_action :set_page_element, only: [:show, :edit, :update, :destroy] - before_action :set_council + before_action :load_permissions + load_and_authorize_resource :page, parent: true, find_by: :url + load_and_authorize_resource :page_element, through: :page - # GET /page_elements - # GET /page_elements.json def index - @page_elements = @page.page_elements - if (@page) - @mainelements = @page.page_elements.where(visible: true,sidebar: false) - @sidebarelements = @page.page_elements.where(visible:true,sidebar: true) - end - if(@mainelemnents) && (@mainelements.count > 1) - @mainelements = @mainelements.sort_by{ |x| x[:displayIndex]} - end - @poster = @council.posts - @rest = @page.page_elements.where(visible: false) end - # GET /page_elements/1 - # GET /page_elements/1.json def show end - # GET /page_elements/new def new - @page_element = PageElement.new() - @path = council_page_page_elements_path(@council) end - # GET /page_elements/1/edit def edit - - @path = council_page_page_element_path(@council,@page_element) end - # POST /page_elements - # POST /page_elements.json def create - @page_element = @page.page_elements.build(page_element_params) - - respond_to do |format| - if @page_element.save - format.html { redirect_to council_page_page_elements_path(@council), notice: 'Elementet skapades.' } - format.json { render action: 'show', status: :created, location: @page_element } - else - format.html { render action: 'new' } - format.json { render json: @page_element.errors, status: :unprocessable_entity } - end + if @page_element.save + redirect_to edit_page_page_element_path(@page, @page_element), + notice: 'Elementet skapades' + else + render action: :new end end - # PATCH/PUT /page_elements/1 - # PATCH/PUT /page_elements/1.json def update - respond_to do |format| - if @page_element.update(page_element_params) - format.html { redirect_to council_page_page_elements_path(@council), notice: 'Elementet uppdaterades.' } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @page_element.errors, status: :unprocessable_entity } - end + if @page_element.update(page_element_params) + redirect_to edit_page_page_element_path(@page, @page_element), + notice: 'Elementet uppdaterades.' + else + render action: :edit end end - # DELETE /page_elements/1 - # DELETE /page_elements/1.json def destroy @page_element.destroy - respond_to do |format| - format.html { redirect_to council_page_page_elements_path(@council) } - format.json { head :no_content } - end + redirect_to @page end private - # Use callbacks to share common setup or constraints between actions. - def set_page_element - @page_element = PageElement.find(params[:id]) - end - def set_council - @council = Council.find_by_url(params[:council_id]) - @page = @council.page - end - # Never trust parameters from the scary internet, only allow the white list through. - def page_element_params - params.fetch(:page_element).permit(:page_id,:displayIndex, :sidebar, :visible,:text, :headline, :border, :name, :pictureR,:picture) - end + def page_element_params + params.fetch(:page_element).permit(:page_id, :displayIndex, :sidebar, + :visible, :text, :headline, :border, + :name, :pictureR, :picture) + end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index c9f727db1..8475c802c 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,84 +1,40 @@ # encoding:UTF-8 class PagesController < ApplicationController - before_action :set_page, only: [:show, :edit, :update, :destroy] + load_permissions_and_authorize_resource find_by: :url - # GET /pages - # GET /pages.json def index - @pages = @page.page_elements end - # GET /pages/1 - # GET /pages/1.json def show - if (@page) - @mainelements = @page.page_elements.where(visible: true,sidebar: false) - @sidebarelements = @page.page_elements.where(visible:true,sidebar: true) - end - if(@mainelemnents) && (@mainelements.count > 1) - @mainelements = @mainelements.sort_by{ |x| x[:displayIndex]} - end - @poster = @council.posts end - # GET /pages/new def new - @page = Page.new end - # GET /pages/1/edit def edit end - # POST /pages - # POST /pages.json def create - @page = Page.new(page_params) - - respond_to do |format| - if @page.save - format.html { redirect_to @page, notice: 'Sidan skapades, success!.' } - format.json { render action: 'show', status: :created, location: @page } - else - format.html { render action: 'new' } - format.json { render json: @page.errors, status: :unprocessable_entity } - end - end + flash[:notice] = 'Sidan skapades, success!.' if @page.save + redirect_to @page end - # PATCH/PUT /pages/1 - # PATCH/PUT /pages/1.json def update - respond_to do |format| - if @page.update(page_params) - format.html { redirect_to @page, notice: 'Sidan uppdaterades, great!' } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @page.errors, status: :unprocessable_entity } - end + if @page.update(page_params) + redirect_to @page, notice: 'Sidan uppdaterades, great!' + else + render action: :edit end end - # DELETE /pages/1 - # DELETE /pages/1.json def destroy @page.destroy - respond_to do |format| - format.html { redirect_to pages_url } - format.json { head :no_content } - end + redirect_to pages_url end private - # Use callbacks to share common setup or constraints between actions. - def set_page - @council = Council.find_by_url(params[:council_id]) - @page = @council.page - end - # Never trust parameters from the scary internet, only allow the white list through. - def page_params - params.fetch(:page).permit(:council_id) - end + def page_params + params.fetch(:page).permit(:council_id, :url, :visible, :title) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 988c4acb2..b854eb99e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,102 +1,63 @@ # encoding:UTF-8 class PostsController < ApplicationController load_permissions_and_authorize_resource - before_action :set_council + load_and_authorize_resource :council, parent: true, find_by: :url + before_action :set_permissions + before_action :set_councils, only: [:new, :edit, :update, :create] + before_action :set_profile, only: [:remove_profile, :add_profile] def remove_profile - @profile = Profile.find_by_id(params[:profile_id]) - @post.profiles.delete(@profile) - respond_to do |format| - format.html { redirect_to council_posts_path(@council), notice: @profile.name.to_s + ' har inte längre posten ' + @post.title.to_s + '.' } - end + @post.remove_profile(@profile) + redirect_to back, + notice: %(#{@profile.full_name} har inte längre posten #{@post.title}.) end - def add_profile_username - @user = User.find_by(username: params[:username]) - if @user != nil - @profile = @user.profile - end - if @profile == nil - respond_to do |format| - format.html { redirect_to council_posts_path(@council), flash: {alert: 'Hittade ingen användare med det användarnamnet.'} } - end - elsif @profile.name.blank? - redirect_to council_posts_path(@council), flash: {alert: 'Användaren :"' + @user.username.to_s + '" måste fylla i fler uppgifter i sin profil.'} - elsif @profile.posts.include?(@post) - redirect_to council_posts_path(@council), flash: {alert: @profile.name.to_s + '(' + @user.username.to_s + ') har redan posten '+@post.title.to_s + '.'} - elsif (@post.limit != nil) && (@post.profiles.size >= @post.limit) - redirect_to council_posts_path(@council), flash: {alert: @post.title.to_s + ' har sitt maxantal.'} + def add_profile + if @post.add_profile(@profile) + flash[:notice] = %(#{@profile.full_print} tilldelades #{@post}) else - @post.profiles << @profile - redirect_to council_posts_path(@council), notice: @profile.name.to_s + ' (' + @profile.user.username.to_s + ') tilldelades posten '+@post.title.to_s + '.' - if (@profile.first_post == nil) - @profile.update(first_post: @post.id) - end + flash[:alert] = %(Tilldelningen gick inte: #{@post.errors.full_messages}) end + redirect_to back end def index - if (@council) - @posts = @council.posts - else - @posts = Post.all - end + @posts = (@council.present?) ? @council.posts : Post.all @post_grid = initialize_grid(@posts) end def new @post = @council.posts.build - @councils = Council.order(title: :asc) + @post_permissions = @post.permissions.collect! { |p| p.id } end def edit @post_permissions = @post.permissions.collect! { |p| p.id } - @councils = Council.order(title: :asc) - @permissions = Permission.all end def create - @councils = Council.order(title: :asc) @post = @council.posts.build(post_params) - respond_to do |format| - if @post.save - format.html { redirect_to council_posts_path(@council), notice: 'Posten skapades!' } - format.json { render action: 'show', status: :created, location: @post } - else - format.html { render action: 'new' } - format.json { render json: @posts.errors, status: :unprocessable_entity } - end + if @post.save + redirect_to council_posts_path(@council), notice: 'Posten skapades!' + else + render action: 'new' end end def update - respond_to do |format| - @post.attributes = post_params - @post.permissions = [] - @post.set_permissions(params[:permissions]) if params[:permissions] - if @post.save - @council2 = Council.find_by_id(params[:post][:council]) - if (@council2) && (@council2.equal?(@council) == false) - @council2.posts << @post - @council = @council2 - end - format.html { redirect_to edit_council_post_path(@council, @post), notice: 'Posten uppdaterades!' } - format.json { head :no_content } - else - @councils = Council.order(title: :asc) - format.html { render action: 'edit' } - format.json { render json: @post.errors, status: :unprocessable_entity } - end + @post.attributes = post_params + @post.permissions = [] + @post.set_permissions(params[:permissions]) if params[:permissions] + if @post.save + redirect_to edit_council_post_path(@post.council, @post), notice: 'Posten uppdaterades!' + else + render action: 'edit' end end def destroy - @post.profiles.clear @post.destroy - respond_to do |format| - format.html { redirect_to council_posts_path(@council) } - format.json { head :no_content } - end + redirect_to council_posts_path(@council) end def display @@ -113,8 +74,22 @@ def post_params :styrelse, :car_rent, :council_id, :permissions) end - def set_council - @council = Council.find_by_url(params[:council_id]) + def set_councils + @councils = Council.order(title: :asc) + end + + def set_permissions + @permissions = Permission.all end -end + def set_profile + if @post.nil? + @post = Post.find_by_id(params[:post_id]) + end + @profile = Profile.find_by_id(params[:profile_id]) + end + + def back + @council.present? ? council_posts_path(@council) : posts_path + end +end diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 5d9a32013..98ff8b7a3 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -10,61 +10,50 @@ def show def edit - redirect_to(:back) unless current_user.profile == @profile # Calls method to set the first_post attribute if not set # /d.wessman @profile.check_posts - rescue ActionController::RedirectBackError - redirect_to root_path + rescue ActionController::RedirectBackError + redirect_to root_path end def update - respond_to do |format| - if @profile.update(profile_params) - format.html { redirect_to @profile, notice: %(#{t(:your_profile)} #{t(:success_update)}) } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @profile.errors, status: :unprocessable_entity } - end + if @profile.update(profile_params) + redirect_to @profile, notice: %(#{t(:your_profile)} #{t(:success_update)}) + else + render action: 'edit' end end def remove_post @post = Post.find(params[:post_id]) @profile.posts.delete(@post) - respond_to do |format| - format.html { redirect_to edit_profile_path(@profile), notice: 'Du har inte längre posten '+@post.title + '.'} - if @profile.posts.count == 0 - @profile.update(first_post: nil) - end - end + redirect_to edit_profile_path(@profile), notice: %(Du har inte längre posten #{@post}.) + @profile.check_posts end # Action to show profile picture only already authenticated def avatar if @profile.avatar? - if(params[:style] == "original" || params[:style] == "medium" || params[:style] == "thumb") - send_file(@profile.avatar.path(params[:style]), filename:@profile.avatar_file_name, type: "image/jpg",disposition: 'inline',x_sendfile: true) + if params[:style] == 'original' || params[:style] == 'medium' || params[:style] == 'thumb' + send_file(@profile.avatar.path(params[:style]), filename: @profile.avatar_file_name, + type: 'image/jpg', disposition: 'inline', x_sendfile: true) else - send_file(@profile.avatar.path(:medium), filename:@profile.avatar_file_name, type: "image/jpg",disposition: 'inline',x_sendfile: true) + send_file(@profile.avatar.path(:medium), filename: @profile.avatar_file_name, + type: 'image/jpg', disposition: 'inline', x_sendfile: true) end end end - private - def authenticate - redirect_to root_path unless current_user && (current_user == @profile.user) - end - # Use callbacks to share common setup or constraints between actions. - def set_profile - @profile = Profile.find_by_id(params[:id]) - end + def search + @search_profiles = Profile.search_names(params[:firstname], params[:lastname]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def profile_params - params.require(:profile).permit(:name,:lastname, :program, :start_year,:avatar,:first_post,:stil_id,:email,:phone) - end + private + def profile_params + params.require(:profile).permit(:name, :lastname, :program, :start_year, + :avatar, :first_post, :stil_id, :email, :phone) + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b6ef1164d..cf051f264 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,70 +1,44 @@ # encoding:UTF-8 -class UsersController < ApplicationController +class UsersController < ApplicationController load_permissions_and_authorize_resource - before_filter :find_user, :only => [:edit, :update,:destroy,:update_password] - before_filter :owner_required, :only => [:edit, :update] - - def change_role - @user = User.find params[:user_id] - @role = Role.find params[:role_id] - @user.update_attribute(:role, @role) - redirect_to users_path - end - - def index - @users = User.all + + def index end - def update_password - respond_to do |format| - if @user.update_with_password(user_params) - format.html { redirect_to :edit_user_registration, notice: 'Användaruppgifter uppdaterades.' } - format.json { head :no_content } - else - format.html{ redirect_to :edit_user_registration , notice: 'Lösenord måste fyllas i för att ändra uppgifter.' } - format.json { head :no_content } - end - end - end + def update_password + if @user.update_with_password(user_params) + redirect_to :edit_user_registration, notice: 'Användaruppgifter uppdaterades.' + else + redirect_to :edit_user_registration, notice: 'Lösenord måste fyllas i för att ändra uppgifter.' + end + end - def update - @user.update(user_params) - flash[:notice] = 'Användare uppdaterades.' - redirect_to edit_user_path @users + def update + if @user.update(user_params) + flash[:notice] = 'Användare uppdaterades.' + redirect_to edit_user_path @user + else + redirect_to :edit_user_registration + end end def destroy - respond_to do |format| - if @user.update_with_password(user_params) - @user.profile.posts.clear - if @user.destroy - - format.html { redirect_to root_url, notice: 'Användare togs bort..' } - format.json { head :no_content } - end - else - format.html{ redirect_to :edit_user_registration , notice: 'Lösenord måste fyllas i för att radera användare.' } - format.json { head :no_content } + if @user.update_with_password(user_params) + @user.profile.posts.clear + if @user.destroy + redirect_to root_url, notice: 'Användare togs bort.' end + else + redirect_to :edit_user_registration, + notice: 'Lösenord måste fyllas i för att radera användare.' end end - private - def user_params - params.require(:user).permit(:username, :email, :password, :password_confirmation, :current_password) - end - def authenticate_admin! - flash[:error] = t('the_role.access_denied') - redirect_to(:back) unless current_user && current_user.moderator?(:users) - rescue ActionController::RedirectBackError - redirect_to root_path - end - def find_user - @user = User.find(params[:id]) + private - # TheRole: You should define OWNER CHECK OBJECT - # When editable object was found - - @owner_check_object = @user + def user_params + params.require(:user).permit(:username, :email, + :password, :password_confirmation, + :current_password) end end diff --git a/app/controllers/work_posts_controller.rb b/app/controllers/work_posts_controller.rb index f50c9ae6e..6741504d5 100644 --- a/app/controllers/work_posts_controller.rb +++ b/app/controllers/work_posts_controller.rb @@ -1,92 +1,56 @@ # encoding:UTF-8 -class WorkPostsController < ApplicationController - - before_filter :authenticate_user! - before_filter :authenticate, only: [:new, :create,:edit,:destroy,:update] +class WorkPostsController < ApplicationController + load_permissions_and_authorize_resource before_action :set_edit - before_action :set_work_post, except: [:index,:new,:create] - - def index @work_posts = WorkPost.publish @work_post_grid = initialize_grid(WorkPost.publish) @not_published_grid = initialize_grid(WorkPost.unpublish) - if(WorkPost.unpublish.count > 0) + if WorkPost.unpublish.count > 0 @edit_grid = true end end - - def show + def show end - + def new - @work_post = WorkPost.new end - + def edit end - + def create - @work_post = WorkPost.new(work_post_params) @work_post.responsible = current_user.profile.id - respond_to do |format| - if @work_post.save - format.html { redirect_to work_posts_path, notice: 'Jobbposten skapades, success!.' } - format.json { render action: 'show', status: :created, location: @work_post } - else - format.html { render action: 'new' } - format.json { render json: @work_post.errors, status: :unprocessable_entity } - end + if @work_post.save + redirect_to work_posts_path, notice: 'Jobbposten skapades, success!.' + else + render action: 'new' end end def update - respond_to do |format| - if @work_post.update(work_post_params) - format.html { redirect_to work_posts_path, notice: 'Jobbposten uppdaterades, gött' } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @work_post.errors, status: :unprocessable_entity } - end + if @work_post.update(work_post_params) + redirect_to work_posts_path, notice: 'Jobbposten uppdaterades, gött' + else + render action: 'edit' end end def destroy @work_post.destroy - respond_to do |format| - format.html { redirect_to work_posts_path,notice: 'Jobbposten raderades.' } - format.json { head :no_content } - end + redirect_to work_posts_path, notice: 'Jobbposten raderades.' end private - def authenticate - flash[:error] = t('the_role.access_denied') - redirect_to(:back) unless current_user.moderator?(:jobbportal) - - rescue ActionController::RedirectBackError - redirect_to root_path - end - def set_edit - if(current_user) && (current_user.moderator?(:jobbportal)) - @edit = true - else - @edit = false - end - end - # Use callbacks to share common setup or constraints between actions. - def set_work_post - @work_post = WorkPost.find_by_id(params[:id]) - if(@work_post == nil) - @work_post = WorkPost.new() - end - end - # Never trust parameters from the scary internet, only allow the white list through. - def work_post_params - params.fetch(:work_post).permit(:title,:description,:company,:deadline,:kind,:for,:visible,:publish,:picture,:category,:link) - end + def set_edit + @edit = can?(:manage, WorkPost) + end + + def work_post_params + params.fetch(:work_post).permit(:title, :description, :company, :deadline, + :kind, :for, :visible, :publish, :picture, :category, :link) + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1d43c0a2a..1efe7c296 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,15 +3,28 @@ def fa_icon(icon_name) content_tag :i, nil, class: ("fa fa-" + icon_name) end + def model_name(model) + if model.instance_of?(Class) + model.model_name.human + end + end + + def models_name(model) + if model.instance_of?(Class) + return model.model_name.human(count: 2) + end + end + def title(page_title) content_for(:title) { page_title } end + def full_title(page_title) - base_title = "F-sektionen" + base_title = 'F-sektionen' if page_title.empty? base_title else - "#{base_title} | #{page_title}" + %(#{base_title} | #{page_title}) end end @@ -24,6 +37,6 @@ def form_group &block e['class'] = 'form-control ' end end - content_tag :div, raw(html.to_html), :class => 'form-group' + content_tag :div, raw(html.to_html), class: 'form-group' end end diff --git a/app/helpers/constants_helper.rb b/app/helpers/constants_helper.rb deleted file mode 100644 index 1fb532fb0..000000000 --- a/app/helpers/constants_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module ConstantsHelper -end diff --git a/app/models/ability.rb b/app/models/ability.rb index ce5a8cfb1..288fefc3a 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -7,29 +7,32 @@ def initialize(user) user.profile ||= Profile.new # Abilities that everyone get. - can :read, News - can :read, Council - can :read, Election + can :read, [News, Council, Page, Election] can :read, Document, public: true + can [:display, :image], Notice + can [:collapse, :display], Post + + # For calendar-subscription + can :export, Event # TODO Should be removed when everyone is required to log in. # /d.wessman 2015-03-28 can [:read, :update_worker, :remove_worker, :authorize], CafeWork - can [:read, :main], Rent + can [:read, :main, :new, :edit, :create, :update, :destroy, :authorize], Rent # Abilities all signed in users get if user.id - can :nominate, Election - can :candidate, Election + can :manage, User, id: user.id + can [:nominate, :candidate], Election can :manage, Candidate, profile_id: user.profile.id + can :manage, Nomination + can [:show, :avatar], Profile can :manage, Profile, user_id: user.id - can :read, Post + can [:read, :display, :hide], Post can :read, Document can :read, :old_gallery - can :read, Event - # TODO We really need to move calendar to its own controller - can :calendar, Event + can [:read, :calendar], Event end # Note: Root access is given dynamically by having a post with permissions :manage, :all diff --git a/app/models/cafe_work.rb b/app/models/cafe_work.rb index a4a631e56..b2ebe4a23 100644 --- a/app/models/cafe_work.rb +++ b/app/models/cafe_work.rb @@ -8,7 +8,7 @@ class CafeWork < ActiveRecord::Base # Validations validates :work_day, :pass, :lp, :lv, presence: true validates :pass, :lp, inclusion: { in: 1..4 } - validates :lv, inclusion: { in: 1..7 } + validates :lv, inclusion: { in: 1..20 } validates :name, :lastname, :phone, :email, presence: true, if: :has_worker? validates :pass, uniqueness: { scope: [:work_day, :lv, :lp, :d_year] } diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 1e2e9d516..847445140 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -7,7 +7,7 @@ class Candidate < ActiveRecord::Base # Validations validates :profile_id, uniqueness: { - scope: [:post_id, :election_id], message: 'har redan en likadan kandidatur' + scope: [:post_id, :election_id], message: I18n.t('candidates.similar_candidate') }, on: :create validates :name, :lastname, :stil_id, :email, :phone, :post, :profile, :election, presence: true @@ -31,26 +31,18 @@ def prepare(user) end def editable? - election.view_status == 2 || post.elected_by == 'Studierådet' + election.view_status == :during || post.elected_by == 'Studierådet' end def p_url - Rails.application.routes.url_helpers.election_candidate_url(id, host: PUBLIC_URL) + Rails.application.routes.url_helpers.candidate_url(id, host: PUBLIC_URL) end def p_path - Rails.application.routes.url_helpers.election_candidate_path(id) + Rails.application.routes.url_helpers.candidate_path(id) end def owner?(user) user.present? && user.profile == profile end - - def editable? - election.view_status == 2 || post.elected_by == 'Studierådet' - end - - def p_url - Rails.application.routes.url_helpers.election_candidate_url(id, host: PUBLIC_URL) - end end diff --git a/app/models/council.rb b/app/models/council.rb index bb9777431..b98d9316e 100644 --- a/app/models/council.rb +++ b/app/models/council.rb @@ -3,8 +3,8 @@ class Council < ActiveRecord::Base # Associations has_one :page, dependent: :destroy - belongs_to :president, foreign_key: :president, class_name: :Post - belongs_to :vice_president, foreign_key: :vice_president, class_name: :Post + belongs_to :president, foreign_key: :president_id, class_name: :Post + belongs_to :vice_president, foreign_key: :vicepresident_id, class_name: :Post has_many :posts has_many :profiles, through: :posts @@ -15,6 +15,16 @@ class Council < ActiveRecord::Base validates :title, :url, presence: true validates :url, uniqueness: true + after_update :check_page + + def check_page + if page.nil? + build_page(url: url, visible: true, title: title).save! + elsif page.url.nil? + page.update!(url: url, visible: true, title: title) + end + end + def to_s title end diff --git a/app/models/election.rb b/app/models/election.rb index 0fa742c7c..92e70178c 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -3,93 +3,97 @@ class Election < ActiveRecord::Base has_many :nominations, dependent: :destroy has_many :candidates, dependent: :destroy has_and_belongs_to_many :posts - + validates :url, presence: true, uniqueness: true def self.current self.order(start: :asc).where(visible: true).first || nil end - # Returns a number to load different views - # 1: before the election opens - # 2: during the election - # 3: after the election + def termin_grid + if (p = posts.termins).count > 0 + initialize_grid(p, name: 'election') + end + end + + def rest_grid + if (p = posts.not_termins).count > 0 + initialize_grid(p, name: 'election') + end + end + # Returns current status # /d.wessman def view_status - if self.start > Time.zone.now - return 1 - elsif (self.start <= Time.zone.now) && (self.end > Time.zone.now) - return 2 + if start > Time.zone.now + return :before + elsif start <= Time.zone.now && self.end > Time.zone.now + return :during else - return 3 + return :after end end # Returns a status text depending on the view_status # /d.wessman def status_text - i = view_status - if i == 1 - return self.text_before - elsif i == 2 - return self.text_during - else - return self.text_after + case view_status + when :before + text_before + when :during + text_during + when :after + text_after end end # Returns a status text for the nominations page # /d.wessman def nomination_status - if view_status != 3 - return "" + if view_status == :after + I18n.t('nominations.status_after') end - return "Det går endast att nominera till poster som inte väljs på Terminsmötet" end + # Returns the current posts # /d.wessman def current_posts - if view_status != 3 - posts - else + if view_status == :after posts.not_termins + else + posts end end # Returns the start_date if before, the end_date if during and none if after. # /d.wessman def countdown - i = view_status - if i == 1 - return self.start - elsif i == 2 - return self.end + case view_status + when :before + start + when :during + self.end end - nil end def candidate_count(post) if post.present? - self.candidates.where(post_id: post.id).count + candidates.where(post_id: post.id).count else 0 end end def can_candidate?(post) - if post.elected_by == "Terminsmötet" && view_status == 2 + if post.elected_by == 'Terminsmötet' && view_status == :during return true - elsif post.elected_by != "Terminsmötet" && view_status != 1 + elsif post.elected_by != 'Terminsmötet' && view_status != :before return true end - return false + + false end def to_param - if (self.url) - self.url - else - self.id - end + (url.present?) ? url : id end end diff --git a/app/models/list.rb b/app/models/list.rb deleted file mode 100644 index b12f786a6..000000000 --- a/app/models/list.rb +++ /dev/null @@ -1,3 +0,0 @@ -# encoding: UTF-8 -class List < ActiveRecord::Base -end diff --git a/app/models/page.rb b/app/models/page.rb index dd736bc20..306f14a82 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,5 +1,23 @@ # encoding: UTF-8 class Page < ActiveRecord::Base -belongs_to :council -has_many :page_elements + # Associations + belongs_to :council + has_many :page_elements + + # Validations + validates :url, uniqueness: true, if: 'url.present?' + + # Scopes + + def main + page_elements.main + end + + def side + page_elements.side + end + + def to_param + url.present? ? url : id + end end diff --git a/app/models/page_element.rb b/app/models/page_element.rb index fa1042717..faeb57982 100644 --- a/app/models/page_element.rb +++ b/app/models/page_element.rb @@ -1,9 +1,16 @@ # encoding: UTF-8 class PageElement < ActiveRecord::Base - belongs_to :page - has_attached_file :picture, - :styles => { original: "4000x4000>", large: "1500x1500>", small: "250x250>",thumb: "100x100>" }, - :path => ":rails_root/public/system/images/sidor/element/:id/:style/:filename", - :url => "/system/images/sidor/element/:id/:style/:filename" - validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/ + belongs_to :page + has_attached_file :picture, + styles: { original: '4000x4000>', large: '1500x1500>', + small: '250x250>', thumb: '100x100>' }, + path: ':rails_root/public/system/images/sidor/element/:id/:style/:filename', + url: '/system/images/sidor/element/:id/:style/:filename' + validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/ + + scope :visible, -> { where(visible: true) } + scope :main, -> { visible.where(sidebar: false).index } + scope :side, -> { visible.where(sidebar: true).index } + scope :index, -> { order(:displayIndex) } + scope :rest, -> { where(visible: false) } end diff --git a/app/models/permission.rb b/app/models/permission.rb index bb7c7236b..5952c7069 100644 --- a/app/models/permission.rb +++ b/app/models/permission.rb @@ -1,5 +1,5 @@ class Permission < ActiveRecord::Base - has_many :posts, through: 'permission_post' + has_many :posts, through: :permission_posts has_many :permission_posts validates :subject_class, :action, presence: true end diff --git a/app/models/post.rb b/app/models/post.rb index c38e69a66..77225639b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -4,33 +4,66 @@ class Post < ActiveRecord::Base belongs_to :council has_and_belongs_to_many :profiles has_many :nominations - has_many :candidates + has_many :candidates has_many :permission_posts - has_many :permissions, through: 'permission_posts' + has_many :permissions, through: :permission_posts # Scopes - scope :studierad, -> {where(elected_by: "Studierådet").order(council_id: :asc)} - scope :termins, -> {where(elected_by: "Terminsmötet").order(council_id: :asc)} + scope :studierad, -> { where(elected_by: 'Studierådet').order(council_id: :asc) } + scope :termins, -> { where(elected_by: 'Terminsmötet').order(council_id: :asc) } - scope :not_termins, -> {where.not(elected_by: "Terminsmötet").order(council_id: :asc)} + scope :not_termins, -> { where.not(elected_by: 'Terminsmötet').order(council_id: :asc) } # Validations - validates_presence_of :limit,:recLimit, :description + validates :limit, :recLimit, :description, presence: true # Scopes - scope :renters, -> {where(car_rent:true)} - + scope :renters, -> { where(car_rent: true) } + + def to_s + title + end + def printLimit - if((recLimit == 0) && (limit == 0)) || (recLimit > limit ) + if recLimit == 0 && limit == 0 || recLimit > limit "*" - elsif(recLimit == limit) && (recLimit > 0) - limit.to_s + " (x)" - elsif(limit > 0) && (recLimit == 0) - limit.to_s - elsif(limit > recLimit) - recLimit.to_s + "-" + limit.to_s + elsif recLimit == limit && recLimit > 0 + %(#{limit} (x)) + elsif limit > 0 && recLimit == 0 + limit + elsif limit > recLimit + %(#{recLimit}- #{limit}) end - end + end + + def limited? + limit > 0 && profiles.count >= limit + end + + def add_profile(profile) + if profile.nil? + errors.add(:profile, I18n.t('errors.messages.not_found')) + return false + end + + if profiles.include?(profile) + errors.add(:profile, I18n.t('posts.already_have_post')) + return false + end + + if limited? + errors.add(:limit, I18n.t('posts.limited')) + return false + end + + profiles << profile + true + end + + def remove_profile(profile) + profiles.delete(profile) + end + def set_permissions(permissions) permissions.each do |id| #find the main permission assigned from the UI diff --git a/app/models/profile.rb b/app/models/profile.rb index b21016f9e..4217892ed 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -6,6 +6,8 @@ class Profile < ActiveRecord::Base has_many :candidates has_many :rents has_many :councils, through: :posts + # TODO Change the first_post field to first_post_id + # belongs_to :first_post, foreign_key: first_post_id, class: Post # Attachment has_attached_file :avatar, @@ -16,17 +18,25 @@ class Profile < ActiveRecord::Base validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ # Only on update! validates :name, :lastname, presence: true, on: :update - validates :start_year, inclusion: { in: 1954..(Time.zone.today.year + 1) }, on: :update + validates :start_year, inclusion: {in: 1954..(Time.zone.today.year + 1)}, on: :update + + scope :search_names, ->(name, lastname) { + where('name LIKE ? AND lastname LIKE ?', "%#{name}%", "%#{lastname}%") + } def full_name "#{name} #{lastname}".strip end + def full_print + "#{full_name} ( User: #{user_id})".strip + end + # Returns all councils the profile belongs to with a Post who is # allowed to rent the car # /d.wessman def car_councils - self.councils.merge(Post.renters) + councils.merge(Post.renters) end # Check if profile has user data (name and lastname) diff --git a/app/views/admin/cafe_works/new.html.erb b/app/views/admin/cafe_works/new.html.erb index 2d2ffb2a4..70452c60c 100644 --- a/app/views/admin/cafe_works/new.html.erb +++ b/app/views/admin/cafe_works/new.html.erb @@ -1,9 +1,9 @@
Titel | +Url | +Synlig | +Öppnar | +Stänger | +Aktuellt val? | +Uppdaterad | +Redigera | +
---|---|---|---|---|---|---|---|
<%= election.title %> | +<%= election.url %> | +<%= election.visible %> | +<%= election.start%> | +<%= election.end%> | +<%= (election == Election.current) ? 'Sant': 'Falskt'%> | +<%= election.updated_at %> | +<%= link_to('Redigera', admin_election_path(election), class: "btn btn-u") %> | +
Namn och e-post måste fyllas i och en post måste väljas.
").fadeIn('slow'); -<%end%> \ No newline at end of file diff --git a/app/views/election_mailer/candidate_email.html.erb b/app/views/election_mailer/candidate_email.html.erb index d07ebf36b..704bafd63 100644 --- a/app/views/election_mailer/candidate_email.html.erb +++ b/app/views/election_mailer/candidate_email.html.erb @@ -10,9 +10,9 @@ <%if (@candidate.election.candidate_mail)%> <%= simple_format(@candidate.election.candidate_mail)%> <%end%> - <%if (@candidate.post.styrelse)%> + <%if @candidate.post.styrelse && @candidate.election.mail_styrelse_link.present?%> <%=link_to(@candidate.election.mail_styrelse_link,@candidate.election.mail_styrelse_link)%>Mvh,
Valberedningen