From 6374c0b3e38dfc74eb1b4a5a1fcc5229eacdcaf2 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Fri, 8 Mar 2013 12:09:17 +0100 Subject: Added SwitchboardEntry scaffold. switchboard has_many switchboard_entries --- app/assets/javascripts/switchboard_entry.js.coffee | 6 ++ app/assets/stylesheets/application.css | 1 + .../stylesheets/switchboard_entries.css.scss | 5 ++ app/controllers/switchboard_entries_controller.rb | 74 ++++++++++++++++++++++ app/helpers/switchboard_entries_helper.rb | 2 + app/models/sip_account.rb | 2 + app/models/switchboard.rb | 2 + app/models/switchboard_entry.rb | 30 +++++++++ app/views/switchboard_entries/_form.html.haml | 7 ++ app/views/switchboard_entries/_form_core.html.haml | 3 + .../switchboard_entries/_index_core.html.haml | 16 +++++ app/views/switchboard_entries/edit.html.haml | 3 + app/views/switchboard_entries/index.html.haml | 6 ++ app/views/switchboard_entries/new.html.haml | 3 + app/views/switchboard_entries/show.html.haml | 22 +++++++ config/locales/views/switchboard_entries/de.yml | 55 ++++++++++++++++ config/locales/views/switchboard_entries/en.yml | 55 ++++++++++++++++ config/routes.rb | 7 +- .../20130307122344_create_switchboard_entries.rb | 15 +++++ .../switchboard_entries_controller_test.rb | 49 ++++++++++++++ test/unit/switchboard_entry_test.rb | 7 ++ 21 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/switchboard_entry.js.coffee create mode 100644 app/assets/stylesheets/switchboard_entries.css.scss create mode 100644 app/controllers/switchboard_entries_controller.rb create mode 100644 app/helpers/switchboard_entries_helper.rb create mode 100644 app/models/switchboard_entry.rb create mode 100644 app/views/switchboard_entries/_form.html.haml create mode 100644 app/views/switchboard_entries/_form_core.html.haml create mode 100644 app/views/switchboard_entries/_index_core.html.haml create mode 100644 app/views/switchboard_entries/edit.html.haml create mode 100644 app/views/switchboard_entries/index.html.haml create mode 100644 app/views/switchboard_entries/new.html.haml create mode 100644 app/views/switchboard_entries/show.html.haml create mode 100644 config/locales/views/switchboard_entries/de.yml create mode 100644 config/locales/views/switchboard_entries/en.yml create mode 100644 db/migrate/20130307122344_create_switchboard_entries.rb create mode 100644 test/functional/switchboard_entries_controller_test.rb create mode 100644 test/unit/switchboard_entry_test.rb diff --git a/app/assets/javascripts/switchboard_entry.js.coffee b/app/assets/javascripts/switchboard_entry.js.coffee new file mode 100644 index 0000000..d14825e --- /dev/null +++ b/app/assets/javascripts/switchboard_entry.js.coffee @@ -0,0 +1,6 @@ +jQuery -> + $('#switchboard_entries').sortable + axis: 'y' + handle: '.handle' + update: -> + $.post($(this).data('update-url'), $(this).sortable('serialize')) \ No newline at end of file diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 44868e4..557bbda 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -16,4 +16,5 @@ *= require softkeys *= require phone_numbers *= require route_elements + *= require switchboard_entries */ diff --git a/app/assets/stylesheets/switchboard_entries.css.scss b/app/assets/stylesheets/switchboard_entries.css.scss new file mode 100644 index 0000000..12b67ee --- /dev/null +++ b/app/assets/stylesheets/switchboard_entries.css.scss @@ -0,0 +1,5 @@ +#switchboard_entries .handle { + font-size: 12px; + color: #777; + cursor: move; +} \ No newline at end of file diff --git a/app/controllers/switchboard_entries_controller.rb b/app/controllers/switchboard_entries_controller.rb new file mode 100644 index 0000000..ef6c72e --- /dev/null +++ b/app/controllers/switchboard_entries_controller.rb @@ -0,0 +1,74 @@ +class SwitchboardEntriesController < ApplicationController + load_and_authorize_resource :switchboard + authorize_resource :switchboard_entry, :through => :switchboard, :except => [:sort] + + def index + @switchboard_entries = @switchboard.switchboard_entries + spread_breadcrumbs + end + + def show + @switchboard_entry = @switchboard.switchboard_entries.find(params[:id]) + spread_breadcrumbs + end + + def new + @switchboard_entry = @switchboard.switchboard_entries.build + @sip_accounts = SipAccount.all - @switchboard.sip_accounts + spread_breadcrumbs + end + + def create + @switchboard_entry = @switchboard.switchboard_entries.build(switchboard_entry_params) + spread_breadcrumbs + if @switchboard_entry.save + redirect_to switchboard_switchboard_entries_path(@switchboard), :notice => t('switchboard_entries.controller.successfuly_created') + else + render :new + end + end + + def edit + @switchboard_entry = @switchboard.switchboard_entries.find(params[:id]) + @sip_accounts = SipAccount.all - @switchboard.sip_accounts + [@switchboard_entry.sip_account] + spread_breadcrumbs + end + + def update + @switchboard_entry = @switchboard.switchboard_entries.find(params[:id]) + if @switchboard_entry.update_attributes(switchboard_entry_params) + redirect_to [@switchboard, @switchboard_entry], :notice => t('switchboard_entries.controller.successfuly_updated') + else + render :edit + end + end + + def destroy + @switchboard_entry = @switchboard.switchboard_entries.find(params[:id]) + @switchboard_entry.destroy + redirect_to switchboard_switchboard_entries_path(@switchboard), :notice => t('switchboard_entries.controller.successfuly_destroyed') + end + + def sort + params[:switchboard_entry].reverse.each do |id| + @switchboard.switchboard_entries.find(id).move_to_top + end + render nothing: true + end + + private + def switchboard_entry_params + params.require(:switchboard_entry).permit(:name, :sip_account_id) + end + + def spread_breadcrumbs + add_breadcrumb t("users.index.page_title"), tenant_users_path(@switchboard.user.current_tenant) + add_breadcrumb @switchboard.user, tenant_user_path(@switchboard.user.current_tenant, @switchboard.user) + add_breadcrumb t("switchboards.index.page_title"), user_switchboards_path(@switchboard.user) + add_breadcrumb @switchboard, user_switchboard_path(@switchboard.user, @switchboard) + add_breadcrumb t("switchboard_entries.index.page_title"), switchboard_switchboard_entries_path(@switchboard) + if @switchboard_entry && !@switchboard_entry.new_record? + add_breadcrumb @switchboard_entry, switchboard_switchboard_entries_path(@switchboard, @switchboard_entry) + end + end +end diff --git a/app/helpers/switchboard_entries_helper.rb b/app/helpers/switchboard_entries_helper.rb new file mode 100644 index 0000000..ca9b1f9 --- /dev/null +++ b/app/helpers/switchboard_entries_helper.rb @@ -0,0 +1,2 @@ +module SwitchboardEntriesHelper +end diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index cdb609d..74a2562 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -41,6 +41,8 @@ class SipAccount < ActiveRecord::Base has_many :call_legs, :class_name => 'Call' has_many :b_call_legs, :class_name => 'Call', :foreign_key => 'b_sip_account_id' + has_many :switchboard_entries, :dependent => :destroy + # Delegations: # delegate :host, :to => :sip_domain, :allow_nil => true diff --git a/app/models/switchboard.rb b/app/models/switchboard.rb index cb0c70f..74e2767 100644 --- a/app/models/switchboard.rb +++ b/app/models/switchboard.rb @@ -7,6 +7,8 @@ class Switchboard < ActiveRecord::Base :uniqueness => {:scope => :user_id} belongs_to :user, :touch => true + has_many :switchboard_entries, :dependent => :destroy + has_many :sip_accounts, :through => :switchboard_entries def to_s self.name.to_s diff --git a/app/models/switchboard_entry.rb b/app/models/switchboard_entry.rb new file mode 100644 index 0000000..d1e9a0c --- /dev/null +++ b/app/models/switchboard_entry.rb @@ -0,0 +1,30 @@ +class SwitchboardEntry < ActiveRecord::Base + # https://github.com/rails/strong_parameters + include ActiveModel::ForbiddenAttributesProtection + + belongs_to :switchboard, :touch => true + belongs_to :sip_account, :touch => true + + validates :switchboard, + :presence => true + + validates :sip_account, + :presence => true + + validates :name, + :uniqueness => {:scope => :switchboard_id}, + :allow_blank => true, + :allow_nil => true + + acts_as_list :scope => [ :switchboard_id ] + + default_scope order(:position) + + def to_s + if self.name.blank? && !self.sip_account.to_s.blank? + self.sip_account.to_s + else + self.name.to_s + end + end +end diff --git a/app/views/switchboard_entries/_form.html.haml b/app/views/switchboard_entries/_form.html.haml new file mode 100644 index 0000000..b3d56ec --- /dev/null +++ b/app/views/switchboard_entries/_form.html.haml @@ -0,0 +1,7 @@ += simple_form_for([@switchboard, @switchboard_entry]) do |f| + = f.error_notification + + = render "form_core", :f => f + + .form-actions + = f.button :submit, conditional_t('switchboard_entries.form.submit') diff --git a/app/views/switchboard_entries/_form_core.html.haml b/app/views/switchboard_entries/_form_core.html.haml new file mode 100644 index 0000000..6f340c2 --- /dev/null +++ b/app/views/switchboard_entries/_form_core.html.haml @@ -0,0 +1,3 @@ +.inputs + = f.association :sip_account, :collection => @sip_accounts, :label => t('switchboard_entries.form.sip_account_id.label'), :hint => conditional_hint('switchboard_entries.form.sip_account_id.hint'), :autofocus => true, :include_blank => false + = f.input :name, :label => t('switchboard_entries.form.name.label'), :hint => conditional_hint('switchboard_entries.form.name.hint') diff --git a/app/views/switchboard_entries/_index_core.html.haml b/app/views/switchboard_entries/_index_core.html.haml new file mode 100644 index 0000000..406db71 --- /dev/null +++ b/app/views/switchboard_entries/_index_core.html.haml @@ -0,0 +1,16 @@ +%table.table.table-striped + %tr + %th + %th= t('switchboard_entries.index.sip_account_id') + %th= t('switchboard_entries.index.name') + %th + + %tbody{ :id => "switchboard_entries", :'data-update-url' => sort_switchboard_switchboard_entries_path(switchboard_entries.first.switchboard) } + - for switchboard_entry in switchboard_entries + = content_tag_for :tr, switchboard_entry do + %td + %span.handle + %i.icon-resize-vertical + %td= switchboard_entry.sip_account + %td= switchboard_entry.name + =render :partial => 'shared/index_view_edit_destroy_part', :locals => {:parent => switchboard_entry.switchboard, :child => switchboard_entry} \ No newline at end of file diff --git a/app/views/switchboard_entries/edit.html.haml b/app/views/switchboard_entries/edit.html.haml new file mode 100644 index 0000000..8885e25 --- /dev/null +++ b/app/views/switchboard_entries/edit.html.haml @@ -0,0 +1,3 @@ +- content_for :title, t("switchboard_entries.edit.page_title") + += render "form" \ No newline at end of file diff --git a/app/views/switchboard_entries/index.html.haml b/app/views/switchboard_entries/index.html.haml new file mode 100644 index 0000000..302b778 --- /dev/null +++ b/app/views/switchboard_entries/index.html.haml @@ -0,0 +1,6 @@ +- content_for :title, t("switchboard_entries.index.page_title") + +- if @switchboard_entries && @switchboard_entries.count > 0 + = render "index_core", :switchboard_entries => @switchboard_entries + += render :partial => 'shared/create_link', :locals => {:parent => @switchboard, :child_class => SwitchboardEntry} \ No newline at end of file diff --git a/app/views/switchboard_entries/new.html.haml b/app/views/switchboard_entries/new.html.haml new file mode 100644 index 0000000..0801a65 --- /dev/null +++ b/app/views/switchboard_entries/new.html.haml @@ -0,0 +1,3 @@ +- content_for :title, t("switchboard_entries.new.page_title") + += render "form" \ No newline at end of file diff --git a/app/views/switchboard_entries/show.html.haml b/app/views/switchboard_entries/show.html.haml new file mode 100644 index 0000000..b519781 --- /dev/null +++ b/app/views/switchboard_entries/show.html.haml @@ -0,0 +1,22 @@ +- content_for :title, t("switchboard_entries.show.page_title") + +.row + .span6 + %table.table.table-striped + %tr + %td + %strong= t('switchboard_entries.show.sip_account_id') + ":" + %td + = @switchboard_entry.sip_account + %tr + %td + %strong= t('switchboard_entries.show.name') + ":" + %td + = @switchboard_entry.name + %tr + %td + %strong= t('switchboard_entries.show.position') + ":" + %td + = @switchboard_entry.position + + = render :partial => 'shared/show_edit_destroy_part', :locals => {:parent => @switchboard, :child => @switchboard_entry } \ No newline at end of file diff --git a/config/locales/views/switchboard_entries/de.yml b/config/locales/views/switchboard_entries/de.yml new file mode 100644 index 0000000..41804b5 --- /dev/null +++ b/config/locales/views/switchboard_entries/de.yml @@ -0,0 +1,55 @@ +de: + switchboard_entries: + name: 'Switchboard-Eintrag' + controller: + successfuly_created: 'Switchboard-Eintrag wurde angelegt.' + successfuly_updated: 'Switchboard-Eintrag wurde aktualisiert.' + successfuly_destroyed: 'Switchboard-Eintrag wurde gelöscht.' + index: + page_title: 'Liste aller Switchboard-Einträge' + switchboard_id: 'Switchboard' + sip_account_id: 'SIP-Account' + name: 'Name' + position: 'Position' + actions: + confirm_destroy: 'Sind Sie sicher, dass Sie folgendes löschen möchten: Switchboard-Eintrag' + destroy: 'Löschen' + edit: 'Bearbeiten' + show: 'Anzeigen' + create: 'Neu anlegen' + create_for: 'Switchboard-Eintrag neu anlegen für %{resource}' + show: + page_title: 'Switchboard-Eintrag bearbeiten' + switchboard_id: 'Switchboard' + sip_account_id: 'SIP-Account' + name: 'Name' + position: 'Position' + actions: + confirm_destroy: 'Sind Sie sicher, dass die dieses Element löschen möchten?' + destroy: 'Löschen' + edit: 'Bearbeiten' + view_all: 'Alle anzeigen' + new: + page_title: 'Switchboard-Eintrag neu anlegen' + actions: + back_to_list: 'Zurück zur Übersicht' + edit: + page_title: 'Switchboard-Eintrag bearbeiten' + actions: + back_to_list: 'Zurück zur Übersicht' + edit: 'Bearbeiten' + view_all: 'Alle anzeigen' + form: + switchboard_id: + label: 'Switchboard' + hint: '' + sip_account_id: + label: 'SIP-Account' + hint: '' + name: + label: 'Name' + hint: '' + position: + label: 'Position' + hint: '' + submit: 'Absenden' \ No newline at end of file diff --git a/config/locales/views/switchboard_entries/en.yml b/config/locales/views/switchboard_entries/en.yml new file mode 100644 index 0000000..71f2fe4 --- /dev/null +++ b/config/locales/views/switchboard_entries/en.yml @@ -0,0 +1,55 @@ +en: + switchboard_entries: + name: 'Switchboardentry' + controller: + successfuly_created: 'Successfully created Switchboardentry.' + successfuly_updated: 'Successfully updated Switchboardentry.' + successfuly_destroyed: 'Successfully destroyed Switchboardentry.' + index: + page_title: 'Listing Switchboardentry' + switchboard_id: 'Switchboard' + sip_account_id: 'Sip account' + name: 'Name' + position: 'Position' + actions: + confirm_destroy: 'Are you sure you want to delete this Switchboardentry?' + destroy: 'Delete' + edit: 'Edit' + show: 'View' + create: 'New' + create_for: 'New Switchboardentry for %{resource}' + show: + page_title: 'Show Switchboardentry' + switchboard_id: 'Switchboard' + sip_account_id: 'Sip account' + name: 'Name' + position: 'Position' + actions: + confirm_destroy: 'Are you sure you want to delete this element?' + destroy: 'Delete' + edit: 'Edit' + view_all: 'View All' + new: + page_title: 'New Switchboardentry' + actions: + back_to_list: 'Back to Index' + edit: + page_title: 'Editing Switchboardentry' + actions: + back_to_list: 'Back to Index' + edit: 'Edit' + view_all: 'View All' + form: + switchboard_id: + label: 'Switchboard' + hint: '' + sip_account_id: + label: 'Sip account' + hint: '' + name: + label: 'Name' + hint: '' + position: + label: 'Position' + hint: '' + submit: 'Submit' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 821cf73..369ec92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,10 @@ Gemeinschaft42c::Application.routes.draw do - resources :switchboards + + resources :switchboards do + resources :switchboard_entries do + collection { post :sort } + end + end resources :restore_jobs diff --git a/db/migrate/20130307122344_create_switchboard_entries.rb b/db/migrate/20130307122344_create_switchboard_entries.rb new file mode 100644 index 0000000..1c7521e --- /dev/null +++ b/db/migrate/20130307122344_create_switchboard_entries.rb @@ -0,0 +1,15 @@ +class CreateSwitchboardEntries < ActiveRecord::Migration + def self.up + create_table :switchboard_entries do |t| + t.integer :switchboard_id + t.integer :sip_account_id + t.string :name + t.integer :position + t.timestamps + end + end + + def self.down + drop_table :switchboard_entries + end +end diff --git a/test/functional/switchboard_entries_controller_test.rb b/test/functional/switchboard_entries_controller_test.rb new file mode 100644 index 0000000..a20f39e --- /dev/null +++ b/test/functional/switchboard_entries_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class SwitchboardEntriesControllerTest < ActionController::TestCase + setup do + @switchboard_entry = switchboard_entries(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:switchboard_entries) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create switchboard_entry" do + assert_difference('SwitchboardEntry.count') do + post :create, switchboard_entry: @switchboard_entry.attributes + end + + assert_redirected_to switchboard_entry_path(assigns(:switchboard_entry)) + end + + test "should show switchboard_entry" do + get :show, id: @switchboard_entry.to_param + assert_response :success + end + + test "should get edit" do + get :edit, id: @switchboard_entry.to_param + assert_response :success + end + + test "should update switchboard_entry" do + put :update, id: @switchboard_entry.to_param, switchboard_entry: @switchboard_entry.attributes + assert_redirected_to switchboard_entry_path(assigns(:switchboard_entry)) + end + + test "should destroy switchboard_entry" do + assert_difference('SwitchboardEntry.count', -1) do + delete :destroy, id: @switchboard_entry.to_param + end + + assert_redirected_to switchboard_entries_path + end +end diff --git a/test/unit/switchboard_entry_test.rb b/test/unit/switchboard_entry_test.rb new file mode 100644 index 0000000..a86c39a --- /dev/null +++ b/test/unit/switchboard_entry_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SwitchboardEntryTest < ActiveSupport::TestCase + def test_should_be_valid + assert SwitchboardEntry.new.valid? + end +end -- cgit v1.2.3