diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb index bc7280df..ed890eec 100644 --- a/app/controllers/admin/application_controller.rb +++ b/app/controllers/admin/application_controller.rb @@ -2,5 +2,9 @@ module Admin class ApplicationController < ::ApplicationController before_filter :authenticate_administrator! layout 'administrator' + private + def set_locale + I18n.locale = :en + end end end diff --git a/app/controllers/admin/lists_controller.rb b/app/controllers/admin/lists_controller.rb index 97a941bc..6d98ca47 100644 --- a/app/controllers/admin/lists_controller.rb +++ b/app/controllers/admin/lists_controller.rb @@ -44,6 +44,7 @@ module Admin # POST /lists.json def create @list = List.new(params[:list]) + @list.supplier_id = params[:list][:supplier_id] respond_to do |format| if @list.save diff --git a/app/controllers/admin/product_categories_controller.rb b/app/controllers/admin/product_categories_controller.rb index 728958ce..3fe32818 100644 --- a/app/controllers/admin/product_categories_controller.rb +++ b/app/controllers/admin/product_categories_controller.rb @@ -44,6 +44,7 @@ module Admin # POST /product_categories.json def create @product_category = ProductCategory.new(params[:product_category]) + @product_category.supplier_id = params[:product_category][:supplier_id] respond_to do |format| if @product_category.save diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 1a428ee0..7ae3be2b 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -44,6 +44,7 @@ module Admin # POST /products.json def create @product = Product.new(params[:product]) + @product.supplier_id = params[:product][:supplier_id] respond_to do |format| if @product.save diff --git a/app/controllers/admin/sections_controller.rb b/app/controllers/admin/sections_controller.rb index c040d0ed..4ead6003 100644 --- a/app/controllers/admin/sections_controller.rb +++ b/app/controllers/admin/sections_controller.rb @@ -44,6 +44,7 @@ module Admin # POST /sections.json def create @section = Section.new(params[:section]) + @section.supplier_id = params[:section][:supplier_id] respond_to do |format| if @section.save diff --git a/app/controllers/admin/tables_controller.rb b/app/controllers/admin/tables_controller.rb index 0b15a202..cb874284 100644 --- a/app/controllers/admin/tables_controller.rb +++ b/app/controllers/admin/tables_controller.rb @@ -44,6 +44,7 @@ module Admin # POST /tables.json def create @table = Table.new(params[:table]) + @table.supplier_id = params[:table][:supplier_id] respond_to do |format| if @table.save diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 54b2a10b..682ac621 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base protect_from_forgery - private +private def broadcast_user(uid, event, data = {}) message = {channel: "/user/#{uid}", data: {event: event, data: data}} diff --git a/app/models/administrator.rb b/app/models/administrator.rb index 3cb208f6..9209aaec 100644 --- a/app/models/administrator.rb +++ b/app/models/administrator.rb @@ -2,4 +2,6 @@ class Administrator include SimplyStored::Couch include Devise::Orm::SimplyStored devise :database_authenticatable, :rememberable #, :recoverable, :rememberable, :trackable, :registerable + + view :by_email, key: :email end diff --git a/app/models/list.rb b/app/models/list.rb index 9ecb2421..d59786b3 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -17,6 +17,8 @@ class List belongs_to :section has_and_belongs_to_many :users, storing_keys: true + attr_protected :supplier_id + validates :table_id, presence: true validates :supplier_id, presence: true diff --git a/app/models/product.rb b/app/models/product.rb index 5681b2c5..1b3f199c 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -10,6 +10,9 @@ class Product belongs_to :supplier # direct! category is an aid has_many :product_orders + attr_protected :supplier_id + + validates :name, presence: true validates :supplier_id, presence: true validates :price, presence: true, numericality: true diff --git a/app/models/product_category.rb b/app/models/product_category.rb index c7287095..1b83cdb5 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -9,6 +9,7 @@ class ProductCategory attr_protected :supplier_id + validates :name, presence: true validates :position, numericality: true validates :supplier_id, presence: true diff --git a/app/models/product_order.rb b/app/models/product_order.rb index aa47b703..f9f6ccf9 100644 --- a/app/models/product_order.rb +++ b/app/models/product_order.rb @@ -8,5 +8,6 @@ class ProductOrder belongs_to :order view :by_product_id, key: :product_id + view :by_order_id, key: :order_id end diff --git a/app/models/section.rb b/app/models/section.rb index 512cd4b0..9b291e24 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -2,7 +2,6 @@ class Section include SimplyStored::Couch property :title - property :path, type: Array, default: [] belongs_to :supplier @@ -12,6 +11,7 @@ class Section attr_protected :supplier_id + validates :title, presence: true validates :supplier_id, presence: true # Probably faster to directly retreive the document and return nil diff --git a/app/views/lists/_form.html.slim b/app/views/admin/lists/_form.html.slim similarity index 90% rename from app/views/lists/_form.html.slim rename to app/views/admin/lists/_form.html.slim index d96acde8..44c01bbc 100644 --- a/app/views/lists/_form.html.slim +++ b/app/views/admin/lists/_form.html.slim @@ -1,4 +1,4 @@ -= form_for @list, html: {class: 'form-horizontal' } do |f| += form_for [:admin, @list], html: {class: 'form-horizontal' } do |f| = render 'error_messages', target: @list .control-group class=(@list.errors[:state].any? ? 'error' : nil) = f.label :state, class: 'control-label' @@ -27,4 +27,4 @@ .form-actions = f.submit nil, class: 'btn btn-primary' ' - = link_to t("helpers.links.cancel"), lists_path, class: 'btn' + = link_to t("helpers.links.cancel"), admin_lists_path, class: 'btn' diff --git a/app/views/lists/edit.html.slim b/app/views/admin/lists/edit.html.slim similarity index 100% rename from app/views/lists/edit.html.slim rename to app/views/admin/lists/edit.html.slim diff --git a/app/views/lists/index.html.slim b/app/views/admin/lists/index.html.slim similarity index 58% rename from app/views/lists/index.html.slim rename to app/views/admin/lists/index.html.slim index 5622111f..7cee85c4 100644 --- a/app/views/lists/index.html.slim +++ b/app/views/admin/lists/index.html.slim @@ -15,18 +15,18 @@ div.page-header= title :index, model_class tbody - @lists.each do |list| tr - td= link_to list.state, list - td= list.needs_help - td= list.needs_payment + td= link_to list.state, [:admin, list] + td= show_boolean list.needs_help + td= show_boolean list.needs_payment td= list.closed_at - td= link_to_if list.table.present?, list.table.try(:number), list.table - td= link_to_if list.supplier.present?, list.supplier.try(:name), list.supplier + td= link_to_if list.table.present?, list.table.try(:number), [:admin, list.table] + td= link_to list.supplier.name, [:admin, list.supplier] td=l list.created_at, format: :short td - = link_to t('helpers.links.edit'), [:edit, list], class: 'btn btn-mini' + = link_to t('helpers.links.edit'), [:edit, :admin, list], class: 'btn btn-mini' ' - = link_to t("helpers.links.destroy"), list, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' + = link_to t("helpers.links.destroy"), [:admin, list], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' - else = no_content_given model_class -= link_to t("helpers.links.new"), new_list_path, class: 'btn btn-primary' += link_to t("helpers.links.new"), new_admin_list_path, class: 'btn btn-primary' diff --git a/app/views/lists/new.html.slim b/app/views/admin/lists/new.html.slim similarity index 100% rename from app/views/lists/new.html.slim rename to app/views/admin/lists/new.html.slim diff --git a/app/views/lists/show.html.slim b/app/views/admin/lists/show.html.slim similarity index 51% rename from app/views/lists/show.html.slim rename to app/views/admin/lists/show.html.slim index fe7800dd..cd9bba9d 100644 --- a/app/views/lists/show.html.slim +++ b/app/views/admin/lists/show.html.slim @@ -12,14 +12,13 @@ dl.dl-horizontal.show-list dd= @list.closed_at - if @list.table.present? dt= Table.model_name.human - dd= link_to @list.table.number, @list.table - - if @list.supplier.present? - dt= Supplier.model_name.human - dd= link_to @list.supplier.name, @list.supplier + dd= link_to @list.table.number, [:admin, @list.table] + dt= Supplier.model_name.human + dd= link_to @list.supplier.name, [:admin, @list.supplier] .form-actions - = link_to t("helpers.links.back"), lists_path, class: 'btn' + = link_to t("helpers.links.back"), admin_lists_path, class: 'btn' ' - = link_to t('helpers.links.edit'), [:edit, @list], class: 'btn' + = link_to t('helpers.links.edit'), [:edit, :admin, @list], class: 'btn' ' - = link_to t("helpers.links.destroy"), @list, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' + = link_to t("helpers.links.destroy"), [:admin, @list], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' diff --git a/app/views/admin/product_categories/_form.html.slim b/app/views/admin/product_categories/_form.html.slim new file mode 100644 index 00000000..fc349f65 --- /dev/null +++ b/app/views/admin/product_categories/_form.html.slim @@ -0,0 +1,12 @@ += simple_form_for [:admin, @product_category], html: {class: 'form-horizontal' } do |f| + = render 'error_messages', target: @product_category + = f.input :name + = f.input :position + .control-group class=(@product_category.errors[:supplier_id].any? ? 'error' : nil) + = f.label :supplier_id, Supplier.model_name.human, class: 'control-label' + .controls + = f.collection_select :supplier_id, @suppliers, :id, :name, include_blank: nil + .form-actions + = f.submit nil, class: 'btn btn-primary' + ' + = link_to t("helpers.links.cancel"), admin_product_categories_path, class: 'btn' diff --git a/app/views/product_categories/edit.html.slim b/app/views/admin/product_categories/edit.html.slim similarity index 53% rename from app/views/product_categories/edit.html.slim rename to app/views/admin/product_categories/edit.html.slim index b68302b0..df3a5cce 100644 --- a/app/views/product_categories/edit.html.slim +++ b/app/views/admin/product_categories/edit.html.slim @@ -1,4 +1,3 @@ - model_class = ProductCategory -.page-header - = title :edit, model_class +.page-header= title :edit, model_class = render 'form' diff --git a/app/views/product_categories/index.html.slim b/app/views/admin/product_categories/index.html.slim similarity index 55% rename from app/views/product_categories/index.html.slim rename to app/views/admin/product_categories/index.html.slim index 9f0566ed..0db1f9d5 100644 --- a/app/views/product_categories/index.html.slim +++ b/app/views/admin/product_categories/index.html.slim @@ -12,15 +12,15 @@ tbody - @product_categories.each do |product_category| tr - td.link= link_to product_category.name, product_category + td.link= link_to product_category.name, [:admin, product_category] td= product_category.position - td.link= link_to_if product_category.supplier.present?, product_category.supplier.try(:name), product_category.supplier + td.link= link_to product_category.supplier.name, [:admin, product_category.supplier] td.timestamp=l product_category.created_at, format: :short td.actions - = link_to t('helpers.links.edit'), [:edit, product_category], class: 'btn btn-mini' + = link_to t('helpers.links.edit'), [:edit, :admin, product_category], class: 'btn btn-mini' ' - = link_to t("helpers.links.destroy"), product_category, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' + = link_to t("helpers.links.destroy"), [:admin, product_category], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' - else = no_content_given model_class -= link_to t("helpers.links.new"), new_product_category_path, class: 'btn btn-primary' += link_to t("helpers.links.new"), new_admin_product_category_path, class: 'btn btn-primary' diff --git a/app/views/product_categories/new.html.slim b/app/views/admin/product_categories/new.html.slim similarity index 53% rename from app/views/product_categories/new.html.slim rename to app/views/admin/product_categories/new.html.slim index 4c3f76bd..8ed30327 100644 --- a/app/views/product_categories/new.html.slim +++ b/app/views/admin/product_categories/new.html.slim @@ -1,4 +1,3 @@ - model_class = ProductCategory -.page-header - = title :new, model_class +.page-header= title :new, model_class = render 'form' diff --git a/app/views/admin/product_categories/show.html.slim b/app/views/admin/product_categories/show.html.slim new file mode 100644 index 00000000..58e7147f --- /dev/null +++ b/app/views/admin/product_categories/show.html.slim @@ -0,0 +1,17 @@ +- model_class = ProductCategory +.page-header= title :show, @product_category + +dl.dl-horizontal.show-list + dt= model_class.human_attribute_name(:name) + dd= @product_category.name + dt= model_class.human_attribute_name(:position) + dd= @product_category.position + dt= Supplier.model_name.human + dd= link_to @product_category.supplier.name, [:admin, @product_category.supplier] + +.form-actions + = link_to t("helpers.links.back"), admin_product_categories_path, class: 'btn' + ' + = link_to t('helpers.links.edit'), [:edit, :admin, @product_category], class: 'btn' + ' + = link_to t("helpers.links.destroy"), [:admin, @product_category], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' diff --git a/app/views/admin/sections/_form.html.slim b/app/views/admin/sections/_form.html.slim new file mode 100644 index 00000000..d5cb8f27 --- /dev/null +++ b/app/views/admin/sections/_form.html.slim @@ -0,0 +1,11 @@ += simple_form_for [:admin, @section], html: {class: 'form-horizontal' } do |f| + = render 'error_messages', target: @section + = f.input :title + .control-group class=(@section.errors[:supplier_id].any? ? 'error' : nil) + = f.label :supplier_id, Supplier.model_name.human, class: 'control-label' + .controls + = f.collection_select :supplier_id, @suppliers, :id, :name, include_blank: nil + .form-actions + = f.submit nil, class: 'btn btn-primary' + ' + = link_to t("helpers.links.cancel"), admin_sections_path, class: 'btn' diff --git a/app/views/sections/edit.html.slim b/app/views/admin/sections/edit.html.slim similarity index 100% rename from app/views/sections/edit.html.slim rename to app/views/admin/sections/edit.html.slim diff --git a/app/views/sections/index.html.slim b/app/views/admin/sections/index.html.slim similarity index 54% rename from app/views/sections/index.html.slim rename to app/views/admin/sections/index.html.slim index 55b82051..02b8f83d 100644 --- a/app/views/sections/index.html.slim +++ b/app/views/admin/sections/index.html.slim @@ -5,22 +5,20 @@ thead tr th= model_class.human_attribute_name(:title) - th= model_class.human_attribute_name(:path) th= Supplier.model_name.human th= model_class.human_attribute_name(:created_at) th=t 'helpers.actions' tbody - @sections.each do |section| tr - td= link_to section.title, section - td= section.path - td= link_to_if section.supplier.present?, section.supplier.try(:name), section.supplier + td= link_to section.title, [:admin, section] + td= link_to_if section.supplier.present?, section.supplier.try(:name), [:admin, section.supplier] td=l section.created_at, format: :short td - = link_to t('helpers.links.edit'), [:edit, section], class: 'btn btn-mini' + = link_to t('helpers.links.edit'), [:edit, :admin, section], class: 'btn btn-mini' ' - = link_to t("helpers.links.destroy"), section, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' + = link_to t("helpers.links.destroy"), [:admin, section], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger' - else = no_content_given model_class -= link_to t("helpers.links.new"), new_section_path, class: 'btn btn-primary' += link_to t("helpers.links.new"), new_admin_section_path, class: 'btn btn-primary' diff --git a/app/views/admin/sections/new.html.slim b/app/views/admin/sections/new.html.slim new file mode 100644 index 00000000..7f3e08ec --- /dev/null +++ b/app/views/admin/sections/new.html.slim @@ -0,0 +1,3 @@ +- model_class = Section +.page-header= title :new, model_class += render 'form' diff --git a/app/views/admin/sections/show.html.slim b/app/views/admin/sections/show.html.slim new file mode 100644 index 00000000..fda1a69a --- /dev/null +++ b/app/views/admin/sections/show.html.slim @@ -0,0 +1,17 @@ +- model_class = Section +.page-header= title :show, @section + +dl.dl-horizontal.show-list + dt= model_class.human_attribute_name(:title) + dd= @section.title + dt= model_class.human_attribute_name(:path) + dd= @section.path.to_json + dt= Supplier.model_name.human + dd= link_to @section.supplier.name, [:admin, @section.supplier] + +.form-actions + = link_to t("helpers.links.back"), admin_sections_path, class: 'btn' + ' + = link_to t('helpers.links.edit'), [:edit, :admin, @section], class: 'btn' + ' + = link_to t("helpers.links.destroy"), [:admin, @section], method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' diff --git a/app/views/layouts/administrator.html.slim b/app/views/layouts/administrator.html.slim index 99aadde8..1f5fd060 100644 --- a/app/views/layouts/administrator.html.slim +++ b/app/views/layouts/administrator.html.slim @@ -35,6 +35,7 @@ html lang="en" b.caret ul.child-menu.dropdown-menu li= link_to Supplier.model_name.human_plural, admin_suppliers_path + li= link_to Section.model_name.human_plural, admin_sections_path li= link_to Table.model_name.human_plural, admin_tables_path li= link_to Product.model_name.human_plural, admin_products_path li= link_to List.model_name.human_plural, admin_lists_path diff --git a/app/views/product_categories/_form.html.slim b/app/views/product_categories/_form.html.slim deleted file mode 100644 index 0a89e31f..00000000 --- a/app/views/product_categories/_form.html.slim +++ /dev/null @@ -1,18 +0,0 @@ -= form_for @product_category, html: {class: 'form-horizontal' } do |f| - = render 'error_messages', target: @product_category - .control-group class=(@product_category.errors[:name].any? ? 'error' : nil) - = f.label :name, class: 'control-label' - .controls - = f.text_field :name, class: 'text_field' - .control-group class=(@product_category.errors[:position].any? ? 'error' : nil) - = f.label :position, class: 'control-label' - .controls - = f.text_field :position, class: 'text_field' - .control-group class=(@product_category.errors[:supplier_id].any? ? 'error' : nil) - = f.label :supplier_id, Supplier.model_name.human, class: 'control-label' - .controls - = f.collection_select :supplier_id, @suppliers, :id, :name, include_blank: nil - .form-actions - = f.submit nil, class: 'btn btn-primary' - ' - = link_to t("helpers.links.cancel"), product_categories_path, class: 'btn' diff --git a/app/views/product_categories/show.html.slim b/app/views/product_categories/show.html.slim deleted file mode 100644 index 89ecd5b9..00000000 --- a/app/views/product_categories/show.html.slim +++ /dev/null @@ -1,18 +0,0 @@ -- model_class = ProductCategory -.page-header= title :show, @product_category - -dl.dl-horizontal.show-list - dt= model_class.human_attribute_name(:name) - dd= @product_category.name - dt= model_class.human_attribute_name(:position) - dd= @product_category.position - - if @product_category.supplier.present? - dt= Supplier.model_name.human - dd= link_to @product_category.supplier.name, @product_category.supplier - -.form-actions - = link_to t("helpers.links.back"), product_categories_path, class: 'btn' - ' - = link_to t('helpers.links.edit'), [:edit, @product_category], class: 'btn' - ' - = link_to t("helpers.links.destroy"), @product_category, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' diff --git a/app/views/sections/_form.html.slim b/app/views/sections/_form.html.slim deleted file mode 100644 index f7566f60..00000000 --- a/app/views/sections/_form.html.slim +++ /dev/null @@ -1,18 +0,0 @@ -= form_for @section, html: {class: 'form-horizontal' } do |f| - = render 'error_messages', target: @section - .control-group class=(@section.errors[:title].any? ? 'error' : nil) - = f.label :title, class: 'control-label' - .controls - = f.text_field :title, class: 'text_field' - .control-group class=(@section.errors[:path].any? ? 'error' : nil) - = f.label :path, class: 'control-label' - .controls - = f.text_field :path, class: 'text_field' - .control-group class=(@section.errors[:supplier_id].any? ? 'error' : nil) - = f.label :supplier_id, Supplier.model_name.human, class: 'control-label' - .controls - = f.collection_select :supplier_id, @suppliers, :id, :name, include_blank: nil - .form-actions - = f.submit nil, class: 'btn btn-primary' - ' - = link_to t("helpers.links.cancel"), sections_path, class: 'btn' diff --git a/app/views/sections/new.html.slim b/app/views/sections/new.html.slim deleted file mode 100644 index 0838474a..00000000 --- a/app/views/sections/new.html.slim +++ /dev/null @@ -1,4 +0,0 @@ -- model_class = Section -.page-header - = title :new, model_class -= render 'form' diff --git a/app/views/sections/show.html.slim b/app/views/sections/show.html.slim deleted file mode 100644 index c4eca468..00000000 --- a/app/views/sections/show.html.slim +++ /dev/null @@ -1,18 +0,0 @@ -- model_class = Section -.page-header= title :show, @section - -dl.dl-horizontal.show-list - dt= model_class.human_attribute_name(:title) - dd= @section.title - dt= model_class.human_attribute_name(:path) - dd= @section.path - - if @section.supplier.present? - dt= Supplier.model_name.human - dd= link_to @section.supplier.name, @section.supplier - -.form-actions - = link_to t("helpers.links.back"), sections_path, class: 'btn' - ' - = link_to t('helpers.links.edit'), [:edit, @section], class: 'btn' - ' - = link_to t("helpers.links.destroy"), @section, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger' diff --git a/config/routes.rb b/config/routes.rb index a0f656e5..956c830f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ Qwaiter::Application.routes.draw do resources :suppliers resources :tables resources :orders + resources :sections resources :lists resources :products diff --git a/lib/generators/bj_controller/spec/spec_generator.rb b/lib/generators/bj_controller/spec/spec_generator.rb index feec651e..2dad1b94 100644 --- a/lib/generators/bj_controller/spec/spec_generator.rb +++ b/lib/generators/bj_controller/spec/spec_generator.rb @@ -31,6 +31,11 @@ module BjController def controller_class_name "#{controller_name.classify.pluralize}Controller" end + + def required_attribute_name + m = model_name.classify.constantize + m.validators.find{|v| v.is_a? ActiveModel::Validations::PresenceValidator }.try(:attributes).try(:first) || m.property_names.find{|k| k !~ /(_id|_at)$/} + end end end end diff --git a/lib/generators/bj_controller/spec/templates/controller_spec.rb b/lib/generators/bj_controller/spec/templates/controller_spec.rb index a837cc8e..375d9fda 100644 --- a/lib/generators/bj_controller/spec/templates/controller_spec.rb +++ b/lib/generators/bj_controller/spec/templates/controller_spec.rb @@ -87,16 +87,14 @@ describe <%= controller_class_name %> do context "valid attributes" do it "located the requested <%= model_name %>" do put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>) + @<%= model_name %>.reload assigns(:<%= model_name %>).should eq(@<%= model_name %>) end it "changes @<%= model_name %>'s attributes" do - attributes = attributes_for(:<%= model_name %>) - attribute_to_change = attributes.keys.find{|k| k !~ /_id$/} - attributes[attribute_to_change] = "ChangedByTest" - put :update, id: @<%= model_name %>, <%= model_name %>: attributes + put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>, <%= required_attribute_name %>: "ChangedByTest") @<%= model_name %>.reload - @<%= model_name %>.send(attribute_to_change).should eq("ChangedByTest") + @<%= model_name %>.<%= required_attribute_name %>.should eq("ChangedByTest") end it "redirects to the updated <%= model_name %>" do @@ -104,5 +102,34 @@ describe <%= controller_class_name %> do response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, @#{model_name}]" : "@#{model_name}" %> end end + + context "invalid attributes" do + it "locates the requested <%= model_name %>" do + put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''} + assigns(:<%= model_name %>).should eq(@<%= model_name %>) + end + + it "re-renders the edit method" do + put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''} + response.should render_template :edit + end + end + end + + describe 'DELETE destroy' do + before :each do + @<%= model_name %> = create :<%= model_name %> + end + + it "deletes the <%= model_name %>" do + expect{ + delete :destroy, id: @<%= model_name %> + }.to change(<%= model_name.classify %>, :count).by(-1) + end + + it "redirects to <%= model_plural_name %>#index" do + delete :destroy, id: @<%= model_name %> + response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, :#{model_plural_name}]" : "#{model_plural_name}_path" %> + end end end diff --git a/spec/acceptance/suppliers/supplier_main_board_spec.rb_spec.rb b/spec/acceptance/suppliers/supplier_main_board_spec.rb_spec.rb index 3ec100a3..67b27de9 100644 --- a/spec/acceptance/suppliers/supplier_main_board_spec.rb_spec.rb +++ b/spec/acceptance/suppliers/supplier_main_board_spec.rb_spec.rb @@ -11,12 +11,14 @@ feature 'Supplier main board spec.rb', %q{ create_supplier 'supplier@qwaiter.com' create_user 'user@qwaiter.com' end + def create_active_list(options = {}) @table = create :table, supplier: @supplier @list = create :list, supplier: @supplier, table: @table, user_ids: [@user.id] - end + context "using javascript", js: true do + scenario 'the active list should be present and contained in row having its id' do create_active_list login_supplier_as 'supplier@qwaiter.com' diff --git a/spec/controllers/admin/lists_controller_spec.rb b/spec/controllers/admin/lists_controller_spec.rb new file mode 100644 index 00000000..b38485c1 --- /dev/null +++ b/spec/controllers/admin/lists_controller_spec.rb @@ -0,0 +1,135 @@ +# encoding: UTF-8 +require 'spec_helper' + +describe Admin::ListsController do + before :each do + @administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret') + sign_in @administrator + end + + describe "GET #index" do + it "populates an array of lists" do + list = create :list + get :index + assigns(:lists).should eq([list]) + end + + it "should render without errors when no objects are present" do + get :index + expect{ render_template :index }.not_to raise_error + end + + it "renders the :index view" do + get :index + response.should render_template :index + end + end + + describe "GET #show" do + it "assigns the requested list to @list" do + list = create :list + get :show, id: list + assigns(:list).should eq(list) + end + + it "renders the #show view" do + list = create :list + get :show, id: list + response.should render_template :show + end + end + + describe "GET #new" do + it "assigns a new list to @list" do + get :new + assigns(:list).should be_a List + end + + it "renders the #show view" do + get :new + response.should render_template :new + end + end + + describe "POST #create" do + context "with valid attributes" do + it "creates a new list" do + expect{ + post :create, list: attributes_for(:list) + }.to change(List, :count).by(1) + end + + it "redirects to the new list" do + post :create, list: attributes_for(:list) + response.should redirect_to [:admin, List.last] + end + end + + context "with invalid attributes" do + it "does not save the new list" do + expect{ + post :create, list: {} + }.to_not change(List, :count) + end + + it "re-renders the new method" do + post :create, list: {} + response.should render_template :new + end + end + end + + describe 'PUT update' do + before :each do + @list = create :list + end + + context "valid attributes" do + it "located the requested list" do + put :update, id: @list, list: attributes_for(:list) + @list.reload + assigns(:list).should eq(@list) + end + + it "changes @list's attributes" do + put :update, id: @list, list: attributes_for(:list, price: 2.71) + @list.reload + @list.price.should eq(2.71) + end + + it "redirects to the updated list" do + put :update, id: @list, list: attributes_for(:list) + response.should redirect_to [:admin, @list] + end + end + + context "invalid attributes" do + it "locates the requested list" do + put :update, id: @list, list: {table_id: ''} + assigns(:list).should eq(@list) + end + + it "re-renders the edit method" do + put :update, id: @list, list: {table_id: ''} + response.should render_template :edit + end + end + end + + describe 'DELETE destroy' do + before :each do + @list = create :list + end + + it "deletes the list" do + expect{ + delete :destroy, id: @list + }.to change(List, :count).by(-1) + end + + it "redirects to lists#index" do + delete :destroy, id: @list + response.should redirect_to [:admin, :lists] + end + end +end diff --git a/spec/controllers/admin/product_categories_controller_spec.rb b/spec/controllers/admin/product_categories_controller_spec.rb new file mode 100644 index 00000000..391b8c2a --- /dev/null +++ b/spec/controllers/admin/product_categories_controller_spec.rb @@ -0,0 +1,140 @@ +# encoding: UTF-8 +require 'spec_helper' + +describe Admin::ProductCategoriesController do + before :each do + @administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret') + sign_in @administrator + end + + describe "GET #index" do + it "populates an array of product_categories" do + product_category = create :product_category + get :index + assigns(:product_categories).should eq([product_category]) + end + + it "should render without errors when no objects are present" do + get :index + expect{ render_template :index }.not_to raise_error + end + + it "renders the :index view" do + get :index + response.should render_template :index + end + end + + describe "GET #show" do + it "assigns the requested product_category to @product_category" do + product_category = create :product_category + get :show, id: product_category + assigns(:product_category).should eq(product_category) + end + + it "renders the #show view" do + product_category = create :product_category + get :show, id: product_category + response.should render_template :show + end + end + + describe "GET #new" do + it "assigns a new product_category to @product_category" do + get :new + assigns(:product_category).should be_a ProductCategory + end + + it "renders the #show view" do + get :new + response.should render_template :new + end + end + + describe "POST #create" do + context "with valid attributes" do + it "creates a new product_category" do + expect{ + post :create, product_category: attributes_for(:product_category) + }.to change(ProductCategory, :count).by(1) + end + + it "redirects to the new product_category" do + post :create, product_category: attributes_for(:product_category) + response.should redirect_to [:admin, ProductCategory.last] + end + end + + context "with invalid attributes" do + it "does not save the new product_category" do + expect{ + post :create, product_category: {} + }.to_not change(ProductCategory, :count) + end + + it "re-renders the new method" do + post :create, product_category: {} + response.should render_template :new + end + end + end + + describe 'PUT update' do + before :each do + @product_category = create :product_category + end + + context "valid attributes" do + + it "located the requested product_category" do + put :update, id: @product_category, product_category: attributes_for(:product_category) + @product_category.reload + assigns(:product_category).should eq(@product_category) + end + + it "changes @product_category's attributes" do + put :update, id: @product_category, product_category: attributes_for(:product_category, name: "ChangedByTest") + @product_category.reload + @product_category.name.should eq("ChangedByTest") + end + + it "redirects to the updated product_category" do + put :update, id: @product_category, product_category: attributes_for(:product_category) + response.should redirect_to [:admin, @product_category] + end + + end + + context "invalid attributes" do + + it "locates the requested @product_category" do + put :update, id: @product_category, product_category: {} + assigns(:product_category).should eq(@product_category) + end + + it "re-renders the edit method" do + put :update, id: @product_category, product_category: {name: ''} + response.should render_template :edit + end + + end + end + + describe 'DELETE destroy' do + before :each do + @product_category = create :product_category + end + + it "deletes the contact" do + expect{ + delete :destroy, id: @product_category + }.to change(ProductCategory, :count).by(-1) + end + + it "redirects to product_categories#index" do + delete :destroy, id: @product_category + response.should redirect_to [:admin, :product_categories] + end + + end +end diff --git a/spec/controllers/admin/products_controller_spec.rb b/spec/controllers/admin/products_controller_spec.rb index 6fd31e74..35cc9375 100644 --- a/spec/controllers/admin/products_controller_spec.rb +++ b/spec/controllers/admin/products_controller_spec.rb @@ -85,24 +85,56 @@ describe Admin::ProductsController do end context "valid attributes" do + it "located the requested product" do put :update, id: @product, product: attributes_for(:product) + @product.reload assigns(:product).should eq(@product) end it "changes @product's attributes" do - attributes = attributes_for(:product) - attribute_to_change = attributes.keys.find{|k| k !~ /_id$/} - attributes[attribute_to_change] = "ChangedByTest" - put :update, id: @product, product: attributes + put :update, id: @product, product: attributes_for(:product, name: "ChangedByTest") @product.reload - @product.send(attribute_to_change).should eq("ChangedByTest") + @product.name.should eq("ChangedByTest") end it "redirects to the updated product" do put :update, id: @product, product: attributes_for(:product) response.should redirect_to [:admin, @product] end + + end + + context "invalid attributes" do + + it "locates the requested @product" do + put :update, id: @product, product: {} + assigns(:product).should eq(@product) + end + + it "re-renders the edit method" do + put :update, id: @product, product: {name: ''} + response.should render_template :edit + end + end end + + describe 'DELETE destroy' do + before :each do + @product = create :product + end + + it "deletes the contact" do + expect{ + delete :destroy, id: @product + }.to change(Product, :count).by(-1) + end + + it "redirects to products#index" do + delete :destroy, id: @product + response.should redirect_to [:admin, :products] + end + + end end diff --git a/spec/controllers/admin/sections_controller_spec.rb b/spec/controllers/admin/sections_controller_spec.rb new file mode 100644 index 00000000..5f4dd66a --- /dev/null +++ b/spec/controllers/admin/sections_controller_spec.rb @@ -0,0 +1,140 @@ +# encoding: UTF-8 +require 'spec_helper' + +describe Admin::SectionsController do + before :each do + @administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret') + sign_in @administrator + end + + describe "GET #index" do + it "populates an array of sections" do + section = create :section + get :index + assigns(:sections).should =~ [section, Section.find_by_title('Room')] + end + + it "should render without errors when no objects are present" do + get :index + expect{ render_template :index }.not_to raise_error + end + + it "renders the :index view" do + get :index + response.should render_template :index + end + end + + describe "GET #show" do + it "assigns the requested section to @section" do + section = create :section + get :show, id: section + assigns(:section).should eq(section) + end + + it "renders the #show view" do + section = create :section + get :show, id: section + response.should render_template :show + end + end + + describe "GET #new" do + it "assigns a new section to @section" do + get :new + assigns(:section).should be_a Section + end + + it "renders the #show view" do + get :new + response.should render_template :new + end + end + + describe "POST #create" do + context "with valid attributes" do + it "creates a new section" do + expect{ + post :create, section: attributes_for(:section) + }.to change(Section, :count).by(2) # attributes_for creates a new standard section, the post action the other + end + + it "redirects to the new section" do + post :create, section: attributes_for(:section, title: 'Section 1 title') + response.should redirect_to [:admin, Section.find_by_title('Section 1 title')] + end + end + + context "with invalid attributes" do + it "does not save the new section" do + expect{ + post :create, section: {} + }.to_not change(Section, :count) + end + + it "re-renders the new method" do + post :create, section: {} + response.should render_template :new + end + end + end + + describe 'PUT update' do + before :each do + @section = create :section + end + + context "valid attributes" do + + it "located the requested section" do + put :update, id: @section, section: attributes_for(:section) + @section.reload + assigns(:section).should eq(@section) + end + + it "changes @section's attributes" do + put :update, id: @section, section: attributes_for(:section, title: "ChangedByTest") + @section.reload + @section.title.should eq("ChangedByTest") + end + + it "redirects to the updated section" do + put :update, id: @section, section: attributes_for(:section) + response.should redirect_to [:admin, @section] + end + + end + + context "invalid attributes" do + + it "locates the requested section" do + put :update, id: @section, section: {title: ''} + assigns(:section).should eq(@section) + end + + it "re-renders the edit method" do + put :update, id: @section, section: {title: ''} + response.should render_template :edit + end + + end + end + + describe 'DELETE destroy' do + before :each do + @section = create :section + end + + it "deletes the contact" do + expect{ + delete :destroy, id: @section + }.to change(Section, :count).by(-1) + end + + it "redirects to sections#index" do + delete :destroy, id: @section + response.should redirect_to [:admin, :sections] + end + + end +end diff --git a/spec/controllers/admin/tables_controller_spec.rb b/spec/controllers/admin/tables_controller_spec.rb new file mode 100644 index 00000000..302ade34 --- /dev/null +++ b/spec/controllers/admin/tables_controller_spec.rb @@ -0,0 +1,140 @@ +# encoding: UTF-8 +require 'spec_helper' + +describe Admin::TablesController do + before :each do + @administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret') + sign_in @administrator + end + + describe "GET #index" do + it "populates an array of tables" do + table = create :table + get :index + assigns(:tables).should eq([table]) + end + + it "should render without errors when no objects are present" do + get :index + expect{ render_template :index }.not_to raise_error + end + + it "renders the :index view" do + get :index + response.should render_template :index + end + end + + describe "GET #show" do + it "assigns the requested table to @table" do + table = create :table + get :show, id: table + assigns(:table).should eq(table) + end + + it "renders the #show view" do + table = create :table + get :show, id: table + response.should render_template :show + end + end + + describe "GET #new" do + it "assigns a new table to @table" do + get :new + assigns(:table).should be_a Table + end + + it "renders the #show view" do + get :new + response.should render_template :new + end + end + + describe "POST #create" do + context "with valid attributes" do + it "creates a new table" do + expect{ + post :create, table: attributes_for(:table) + }.to change(Table, :count).by(1) + end + + it "redirects to the new table" do + post :create, table: attributes_for(:table) + response.should redirect_to [:admin, Table.last] + end + end + + context "with invalid attributes" do + it "does not save the new table" do + expect{ + post :create, table: {number: 0} + }.to_not change(Table, :count) + end + + it "re-renders the new method" do + post :create, table: {number: 0} + response.should render_template :new + end + end + end + + describe 'PUT update' do + before :each do + @table = create :table + end + + context "valid attributes" do + + it "located the requested table" do + put :update, id: @table, table: attributes_for(:table) + @table.reload + assigns(:table).should eq(@table) + end + + it "changes @table's attributes" do + put :update, id: @table, table: attributes_for(:table, number: "44") + @table.reload + @table.number.should eq(44) + end + + it "redirects to the updated table" do + put :update, id: @table, table: attributes_for(:table) + response.should redirect_to [:admin, @table] + end + + end + + context "invalid attributes" do + + it "locates the requested table" do + put :update, id: @table, table: {number: '0'} + assigns(:table).should eq(@table) + end + + it "re-renders the edit method" do + put :update, id: @table, table: {number: '0'} + response.should render_template :edit + end + + end + end + + describe 'DELETE destroy' do + before :each do + @table = create :table + end + + it "deletes the contact" do + expect{ + delete :destroy, id: @table + }.to change(Table, :count).by(-1) + end + + it "redirects to tables#index" do + delete :destroy, id: @table + response.should redirect_to [:admin, :tables] + end + + end +end diff --git a/spec/factories/list_factory.rb b/spec/factories/list_factory.rb index 2eca981d..df6b233d 100644 --- a/spec/factories/list_factory.rb +++ b/spec/factories/list_factory.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :list do association :table - association :supplier + association :supplier #TODO warning! this may create a different supplier than the one created by the associated table end end diff --git a/spec/factories/product_category_factory.rb b/spec/factories/product_category_factory.rb new file mode 100644 index 00000000..6e748693 --- /dev/null +++ b/spec/factories/product_category_factory.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :product_category do + sequence(:name){|i| "ProductCategory#{i}"} + sequence(:position){|i| i} + association :supplier + end +end diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index 8eb1ae09..a03f8ffd 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -1,5 +1,6 @@ FactoryGirl.define do factory :product do + sequence(:name){|i| "Product#{i}"} price 34.95 association :supplier end diff --git a/spec/factories/section_factory.rb b/spec/factories/section_factory.rb index ff8ab951..dd30e37f 100644 --- a/spec/factories/section_factory.rb +++ b/spec/factories/section_factory.rb @@ -1,5 +1,6 @@ FactoryGirl.define do factory :section do + sequence(:title){|i| "Section #{i} title"} association :supplier end end