From 1172190dbd2646f64d42b3a0a9dd2cffd64be935 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E6=9D=BE=E6=BE=A4=20=E5=A4=AA=E9=83=8E?= Date: Tue, 30 Mar 2010 18:03:50 +0900 Subject: [PATCH] =?utf8?q?2.0=E9=96=8B=E7=99=BA:=E4=BB=96=E5=BA=97?= =?utf8?q?=E8=88=97=E5=AF=BE=E5=BF=9C=20-=20Retailer=E3=83=A2=E3=83=87?= =?utf8?q?=E3=83=AB=E3=81=AE=E8=BF=BD=E5=8A=A0=20-=20=E7=AE=A1=E7=90=86?= =?utf8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E6=A4=9C=E7=B4=A2=E5=88=86=E9=9B=A2?= =?utf8?q?=E4=BD=9C=E6=A5=AD=20-=20=E5=95=86=E5=93=81=E7=99=BB=E9=8C=B2?= =?utf8?q?=E5=95=86=E5=93=81=E3=83=9E=E3=82=B9=E3=82=BF=E7=99=BB=E9=8C=B2?= =?utf8?q?=E5=89=8D=E3=81=BE=E3=81=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- README | 2 +- app/controllers/admin/products_controller.rb | 4 ++ app/controllers/admin/return_items_controller.rb | 24 +++++++- app/controllers/admin/stock_base_controller.rb | 5 ++ app/controllers/admin/stock_csv_controller.rb | 17 +++++- .../admin/stock_histories_controller.rb | 7 ++- .../admin/style_categories_controller.rb | 12 ++++ app/controllers/admin/styles_controller.rb | 11 +++- app/controllers/admin/suppliers_controller.rb | 6 ++ app/controllers/admin/totals_controller.rb | 3 +- app/models/admin_user.rb | 11 ++++ app/models/category.rb | 6 +- app/models/customer_search_form.rb | 1 - app/models/delivery_trader.rb | 2 + app/models/product.rb | 27 +++++++- app/models/product_access_log.rb | 1 - app/models/recommend.rb | 1 - app/models/retailer.rb | 10 +++ app/models/return_item_search_form.rb | 4 ++ app/models/stock_search_form.rb | 3 + app/models/style.rb | 10 +-- app/models/supplier.rb | 9 ++- app/models/supplier_search_form.rb | 6 +- app/views/admin/admin_users/_form.html.erb | 7 +++ app/views/admin/admin_users/index.html.erb | 2 + app/views/admin/admin_users/new.html.erb | 1 - app/views/admin/products/_form.html.erb | 13 +++- app/views/admin/style_categories/index.html.erb | 2 +- app/views/admin/styles/_form.html.erb | 1 + app/views/admin/suppliers/_form.html.erb | 1 + config/locales/translation_ja.yml | 8 +++ db/migrate/20100315054019_create_retailers.rb | 26 ++++++++ ...0100315062449_add_retailer_id_to_many_tables.rb | 23 +++++++ .../admin/admin_users_controller_spec.rb | 3 +- .../admin/new_informations_controller_spec.rb | 1 - spec/controllers/admin/products_controller_spec.rb | 71 ++++++++++++++++++++-- .../admin/return_items_controller_spec.rb | 26 ++++++++ spec/controllers/admin/shops_controller_spec.rb | 22 ++++++- .../admin/stock_base_controller_spec.rb | 9 +++ .../controllers/admin/stock_csv_controller_spec.rb | 28 +++++++++ .../admin/stock_histories_controller_spec.rb | 20 ++++++ spec/controllers/admin/stock_in_controller_spec.rb | 4 +- .../admin/style_categories_controller_spec.rb | 21 +++++++ spec/controllers/admin/styles_controller_spec.rb | 59 +++++++++++++++++- .../controllers/admin/suppliers_controller_spec.rb | 42 ++++++++++++- spec/fixtures/admin_users.yml | 39 +++++++++++- spec/fixtures/delivery_traders.yml | 2 + spec/fixtures/products.yml | 1 + spec/fixtures/retailers.yml | 6 ++ spec/fixtures/style_categories.yml | 6 ++ spec/fixtures/styles.yml | 5 ++ spec/fixtures/suppliers.yml | 18 +++++- spec/models/admin_user_spec.rb | 17 +++++- spec/models/category_spec.rb | 3 +- spec/models/delivery_trader_spec.rb | 2 +- spec/models/product_spec.rb | 9 ++- spec/models/retailer_spec.rb | 18 ++++++ spec/models/style_spec.rb | 21 ++++++- spec/models/supplier_spec.rb | 33 +++++++++- spec/product_csv_upload_for_spec.csv | 6 +- spec/product_sample.csv | 4 +- test/fixtures/retailers.yml | 7 +++ test/unit/retailer_test.rb | 8 +++ 63 files changed, 723 insertions(+), 54 deletions(-) create mode 100644 app/models/retailer.rb create mode 100644 db/migrate/20100315054019_create_retailers.rb create mode 100644 db/migrate/20100315062449_add_retailer_id_to_many_tables.rb create mode 100644 spec/fixtures/retailers.yml create mode 100644 spec/models/retailer_spec.rb create mode 100644 test/fixtures/retailers.yml create mode 100644 test/unit/retailer_test.rb diff --git a/README b/README index dd076b3..752385e 100644 --- a/README +++ b/README @@ -140,7 +140,7 @@ $ sudo gem install postgres # /etc/init.d/postgresql start # su - postgres $ psql template1 -# alert user postgres with password 'xxxx'; +# alter user postgres with password 'xxxx'; # q\ $ createuser ec Shall the new role be a superuser? (y/n) y diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 5f31bf3..086dfbe 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -205,6 +205,10 @@ class Admin::ProductsController < Admin::BaseController end def get_search_form(actual_flg=false) + unless session[:admin_user].master_shop? + addparam = {'retailer_id' => session[:admin_user].retailer_id} + params[:search].merge! addparam unless params[:search].nil? + end @search = SearchForm.new(params[:search]) @search, @search_list = Product.get_conditions(@search, params, actual_flg) end diff --git a/app/controllers/admin/return_items_controller.rb b/app/controllers/admin/return_items_controller.rb index d524887..dda3040 100644 --- a/app/controllers/admin/return_items_controller.rb +++ b/app/controllers/admin/return_items_controller.rb @@ -67,6 +67,7 @@ class Admin::ReturnItemsController < Admin::BaseController end def search + add_retailer_condition @condition = ReturnItemSearchForm.new(params[:condition]) unless @condition.valid? render :action => :index @@ -107,7 +108,8 @@ class Admin::ReturnItemsController < Admin::BaseController if params[:id].blank? render :status => :not_found end - rows = ReturnItem.find(:all).map do |ri| + condition, join = get_csv_condition + rows = ReturnItem.find(:all, :conditions => flatten_conditions(condition), :joins => join).map do |ri| a = [] a << ri.product_id a << ri.product_style.code @@ -132,6 +134,7 @@ class Admin::ReturnItemsController < Admin::BaseController private def get_return_items + add_retailer_condition @condition = ReturnItemSearchForm.new(params[:condition]) unless @condition.valid? render :action => :index @@ -142,7 +145,8 @@ class Admin::ReturnItemsController < Admin::BaseController :page => params[:page], :per_page => @condition.per_page || 10, :conditions => flatten_conditions(@search_list), - :order => "id" + :include => [:product], + :order => "return_items.id" } @return_items = ReturnItem.paginate(find_options) end @@ -151,4 +155,20 @@ class Admin::ReturnItemsController < Admin::BaseController url_for(:action => :csv, :id => date.strftime('%Y%m%d_%H%M%S'),:format => "csv") end + def add_retailer_condition + unless session[:admin_user].master_shop? + addparam = {'retailer_id' => session[:admin_user].retailer_id} + params[:condition].merge! addparam unless params[:condition].nil? + end + end + + def get_csv_condition + condition = [] + unless session[:admin_user].master_shop? + condition << ["products.retailer_id = ?", session[:admin_user].retailer_id] + return condition, "LEFT JOIN product_styles ON product_styles.id = return_items.product_style_id " + "LEFT JOIN products ON products.id = product_styles.product_id " + end + return condition, nil + end + end diff --git a/app/controllers/admin/stock_base_controller.rb b/app/controllers/admin/stock_base_controller.rb index 13cba0b..8a99df5 100644 --- a/app/controllers/admin/stock_base_controller.rb +++ b/app/controllers/admin/stock_base_controller.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- #在庫管理の親クラス #共通ロジック class Admin::StockBaseController < Admin::BaseController @@ -5,6 +6,10 @@ class Admin::StockBaseController < Admin::BaseController #検索 def search + unless session[:admin_user].master_shop? + addparam = {'retailer_id' => session[:admin_user].retailer_id} + params[:condition].merge! addparam unless params[:condition].nil? + end @condition = StockSearchForm.new(params[:condition]) unless @condition.valid? render :action => "index" diff --git a/app/controllers/admin/stock_csv_controller.rb b/app/controllers/admin/stock_csv_controller.rb index 221e389..e4f8659 100644 --- a/app/controllers/admin/stock_csv_controller.rb +++ b/app/controllers/admin/stock_csv_controller.rb @@ -28,9 +28,11 @@ class Admin::StockCsvController < Admin::BaseController def csv # params[:id] はページキャッシュのキーにするだけで抽出条件にはしない if params[:id].blank? - render :status => :not_found + render :file => 'public/404.html', :status => :not_found + return end - rows = ProductStyle.find(:all).map do |ps| + condition, join = get_condition + rows = ProductStyle.find(:all, :conditions => flatten_conditions(condition), :joins => join).map do |ps| a = [] a << ps.code a << ps.product.name @@ -54,5 +56,14 @@ class Admin::StockCsvController < Admin::BaseController def url_for_date(date) url_for(:action => :csv, :id => date.strftime('%Y%m%d_%H%M%S'),:format => "csv") - end + end + + def get_condition + condition = [] + unless session[:admin_user].master_shop? + condition << ["products.retailer_id = ?", session[:admin_user].retailer_id] + return condition, "LEFT JOIN products ON products.id = product_styles.product_id " + end + return condition, nil + end end diff --git a/app/controllers/admin/stock_histories_controller.rb b/app/controllers/admin/stock_histories_controller.rb index ff695d9..0fc9c7b 100644 --- a/app/controllers/admin/stock_histories_controller.rb +++ b/app/controllers/admin/stock_histories_controller.rb @@ -4,6 +4,10 @@ class Admin::StockHistoriesController < Admin::BaseController before_filter :admin_permission_check_stock def search + unless session[:admin_user].master_shop? + addparam = {'retailer_id' => session[:admin_user].retailer_id} + params[:condition].merge! addparam unless params[:condition].nil? + end @condition = StockSearchForm.new(params[:condition]) unless @condition.valid? render :action => "index" @@ -15,7 +19,8 @@ class Admin::StockHistoriesController < Admin::BaseController :page => params[:page], :per_page => @condition.per_page || 10, :conditions => flatten_conditions(@search_list), - :order => "id" + :include => [:product], + :order => "stock_histories.id" } @stock_histories = StockHistory.paginate(find_options) end diff --git a/app/controllers/admin/style_categories_controller.rb b/app/controllers/admin/style_categories_controller.rb index bc989e3..75cd0cc 100644 --- a/app/controllers/admin/style_categories_controller.rb +++ b/app/controllers/admin/style_categories_controller.rb @@ -35,4 +35,16 @@ class Admin::StyleCategoriesController < Admin::BaseController redirect_to :action => :index, :style_id => params[:style_id] end + private + def object + if params[:id] + style_category = StyleCategory.find_by_id(params[:id]) + raise ActiveRecord::RecordNotFound unless style_category.style.retailer_id == session[:admin_user].retailer_id + elsif params[:style_category] && params[:style_category][:style_id] + style = Style.find(:all, :conditions => ["id = ? and retailer_id = ? ", params[:style_category][:style_id], session[:admin_user].retailer_id]) + raise ActiveRecord::RecordNotFound if style.nil? or style == [] + end + super + end + end diff --git a/app/controllers/admin/styles_controller.rb b/app/controllers/admin/styles_controller.rb index 167b775..2f2eaf7 100644 --- a/app/controllers/admin/styles_controller.rb +++ b/app/controllers/admin/styles_controller.rb @@ -3,8 +3,8 @@ class Admin::StylesController < Admin::BaseController before_filter :admin_permission_check_standard index.before do - @styles = Style.find(:all, :order => "position") - @style = Style.find_by_id(params[:id]) || Style.new + @styles = Style.find(:all, :conditions=>["retailer_id = ? ", session[:admin_user].retailer_id], :order => "position") + @style = Style.find(:last, :conditions=>["id = ? and retailer_id = ? ", params[:id], session[:admin_user].retailer_id]) || Style.new end new_action.wants.html do @@ -17,7 +17,7 @@ class Admin::StylesController < Admin::BaseController end action.failure.wants.html do - @styles = Style.find(:all, :order => "position") + @styles = Style.find(:all, :conditions=>["retailer_id = ? ", session[:admin_user].retailer_id], :order => "position") render :action => "index" end end @@ -32,4 +32,9 @@ class Admin::StylesController < Admin::BaseController redirect_to :action => :index end + private + def object + @object ||= Style.find(:last, :conditions => ["id = ? and retailer_id = ? ", params[:id], session[:admin_user].retailer_id]) + end + end diff --git a/app/controllers/admin/suppliers_controller.rb b/app/controllers/admin/suppliers_controller.rb index 9f479ef..2709a30 100644 --- a/app/controllers/admin/suppliers_controller.rb +++ b/app/controllers/admin/suppliers_controller.rb @@ -6,6 +6,12 @@ class Admin::SuppliersController < Admin::BaseController before_filter :check_default,:only => [:edit,:confirm] def search + + if params[:condition].nil? + render :action => "index" + return + end + params[:condition].merge!({"retailer_id" => session[:admin_user].retailer_id}) unless params[:condition].nil? @condition = SupplierSearchForm.new(params[:condition]) unless @condition.valid? render :action => "index" diff --git a/app/controllers/admin/totals_controller.rb b/app/controllers/admin/totals_controller.rb index 0d1fe60..651fde1 100644 --- a/app/controllers/admin/totals_controller.rb +++ b/app/controllers/admin/totals_controller.rb @@ -26,7 +26,8 @@ class Admin::TotalsController < Admin::BaseController begin @records = @agent.get_records(params) rescue => e - p e + logger.error e.message + e.backtrace.each{|bt|logger.error(bt)} end @total = @agent.total begin diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index 008ebc3..fd80f87 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -5,6 +5,7 @@ class AdminUser < ActiveRecord::Base acts_as_list belongs_to :authority + belongs_to :retailer def self.validates_uniqueness_of(*attr_names) configuration = { :message => I18n.translate("activerecord.errors.messages")[:taken], :case_sensitive => true } @@ -69,6 +70,8 @@ class AdminUser < ActiveRecord::Base validates_presence_of :authority validates_presence_of :login_id + + validates_presence_of :retailer validates_length_of :login_id, :maximum => 15 validates_format_of :login_id, :with => /^[a-zA-Z0-9]*$/ validates_uniqueness_of :login_id @@ -103,6 +106,14 @@ class AdminUser < ActiveRecord::Base [columns, titles] end + # + # 管理者ユーザの販売元がマスターショップであるかどうかを判定する + # + def master_shop? + return true if retailer_id == Retailer::DEFAULT_ID + return false + end + protected def crypt_password diff --git a/app/models/category.rb b/app/models/category.rb index 978bc42..0b678f1 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -114,7 +114,7 @@ class Category < ActiveRecord::Base #1.エントリー def self.renew_children_ids_with_command begin - p "batch update start..." + logger.info "batch update start..." Category.transaction { #clear all Category.clear_children_ids @@ -130,9 +130,9 @@ class Category < ActiveRecord::Base Category.childs_re_position(category) end } - p "batch update end..." + logger.info "batch update end..." rescue - p "batch update error.rollback..." + logger.info "batch update error.rollback..." end end diff --git a/app/models/customer_search_form.rb b/app/models/customer_search_form.rb index 85c855b..71ba201 100644 --- a/app/models/customer_search_form.rb +++ b/app/models/customer_search_form.rb @@ -51,7 +51,6 @@ class CustomerSearchForm < SearchForm CSV::Writer.generate(f) do | writer | writer << col_names customers.each do |c| -# p c arr = [] syms.each do |sym| if sym == "sex".to_sym diff --git a/app/models/delivery_trader.rb b/app/models/delivery_trader.rb index 1f0c64e..9d94a06 100644 --- a/app/models/delivery_trader.rb +++ b/app/models/delivery_trader.rb @@ -7,8 +7,10 @@ class DeliveryTrader < ActiveRecord::Base has_many :delivery_fees, :dependent => :destroy, :order => :prefecture_id has_many :payments has_many :orders + belongs_to :retailer validates_presence_of :name + validates_presence_of :retailer validates_length_of :name,:url, :maximum => 50 diff --git a/app/models/product.rb b/app/models/product.rb index 14242e5..fcb2835 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -24,6 +24,7 @@ class Product < ActiveRecord::Base has_many :order_details has_one :campaign belongs_to :supplier + belongs_to :retailer validates_length_of :name , :maximum => 50 validates_length_of :name , :minimum => 1 @@ -35,6 +36,7 @@ class Product < ActiveRecord::Base validates_presence_of :description validates_presence_of :introduction validates_presence_of :supplier + validates_presence_of :retailer validates_associated :sub_products attr_accessor :small_resource_path @@ -127,6 +129,9 @@ class Product < ActiveRecord::Base def supplier_name self.supplier && self.supplier.name end + def retailer_name + self.retailer && self.retailer.name + end # 送料無料? def free_delivery? statuses.exists?(['name=?', '送料無料']) @@ -263,6 +268,9 @@ class Product < ActiveRecord::Base unless search.sale_start_at_end.blank? search_list << ["products.sale_start_at <= ?", search.sale_start_at_end + 1.day] end + unless search.retailer_id.blank? + search_list << ["products.retailer_id = ?", search.retailer_id] + end end unless params["product_status_ids"].blank? product_status = ProductStatus.find(:all, :select => "distinct product_id", :conditions => "status_id IN (#{ params["product_status_ids"].join(",") })" ) @@ -292,7 +300,7 @@ class Product < ActiveRecord::Base end elsif column.to_s == "delivery_dates_label" product.delivery_dates_label - elsif ![:small_resource_path,:medium_resource_path,:large_resource_path,:category_name,:delivery_dates_label,:supplier_name].include?(column)&& Product.columns_hash[column.to_s].type == :datetime + elsif ![:small_resource_path,:medium_resource_path,:large_resource_path,:category_name,:delivery_dates_label,:supplier_name,:retailer_name].include?(column)&& Product.columns_hash[column.to_s].class == :datetime (product[column] + (60*60*9)).strftime("%Y-%m-%d %H:%M") if product[column] else product[column] || product.send(column) @@ -386,6 +394,8 @@ class Product < ActiveRecord::Base product.free_comment = arr[29] setDelivery_dates(product,arr[30]) setSupplierId(product,arr[31]) + # todo: csvupload retailer対応 + setRetailerId(product,arr[32]) end def setPermit(product, permit) @@ -411,6 +421,19 @@ class Product < ActiveRecord::Base end end end + + def setRetailerId(product, name) + if name.blank? + product.retailer_id = Retailer::DEFAULT_ID + else + r = Retailer.find_by_name(name) + if !r.blank? + product.retailer_id = r.id + else + raise ActiveRecord::RecordNotFound + end + end + end #画像データセット def setImageId(product,arr) #画像IDと画像パスの項目を別々に設定して画像パスがあった場合は、その先にある画像を登録し、なかった場合は画像IDを登録する @@ -530,6 +553,7 @@ class Product < ActiveRecord::Base :free_comment, :delivery_dates_label, :supplier_name, + :retailer_name, :created_at, :updated_at ] @@ -569,6 +593,7 @@ class Product < ActiveRecord::Base :free_comment => "フリー入力", :delivery_dates_label => "配送日", :supplier_name => "仕入先名", + :retailer_name => "販売元名", :created_at => "登録日", :updated_at => "更新日" } diff --git a/app/models/product_access_log.rb b/app/models/product_access_log.rb index eb7afa7..4a0334a 100644 --- a/app/models/product_access_log.rb +++ b/app/models/product_access_log.rb @@ -27,7 +27,6 @@ class ProductAccessLog < ActiveRecord::Base product_access_log.save! } rescue TimeoutError => e - p "err: #{ e }" logger.info "err: #{ e }" return false rescue diff --git a/app/models/recommend.rb b/app/models/recommend.rb index 7c92232..1cc4b3d 100644 --- a/app/models/recommend.rb +++ b/app/models/recommend.rb @@ -66,7 +66,6 @@ class Recommend < ActiveRecord::Base end return true rescue TimeoutError => e - p "err: #{ e }" logger.info "err: #{ e }" return false rescue diff --git a/app/models/retailer.rb b/app/models/retailer.rb new file mode 100644 index 0000000..a0d13bd --- /dev/null +++ b/app/models/retailer.rb @@ -0,0 +1,10 @@ +# -*- coding: undecided -*- +class Retailer < ActiveRecord::Base + + has_many :product + validates_presence_of :name + + #DEFAULT_IDは標準の販売元として利用 + DEFAULT_ID = 1 + +end diff --git a/app/models/return_item_search_form.rb b/app/models/return_item_search_form.rb index 312f704..4dba538 100644 --- a/app/models/return_item_search_form.rb +++ b/app/models/return_item_search_form.rb @@ -49,6 +49,10 @@ class ReturnItemSearchForm < SearchForm unless search.returned_at_to.blank? search_list << ["return_items.returned_at <= ?", search.returned_at_to] end + unless search.retailer_id.blank? + search_list << ["products.retailer_id = ?", search.retailer_id] + end + end search_list end diff --git a/app/models/stock_search_form.rb b/app/models/stock_search_form.rb index 630ef93..d1d5aa8 100644 --- a/app/models/stock_search_form.rb +++ b/app/models/stock_search_form.rb @@ -50,6 +50,9 @@ class StockSearchForm < SearchForm unless search.moved_at_to.blank? search_list << ["stock_histories.moved_at <= ?", search.moved_at_to] end + unless search.retailer_id.blank? + search_list << ["products.retailer_id = ?", search.retailer_id] + end end search_list end diff --git a/app/models/style.rb b/app/models/style.rb index 2240f29..763218b 100644 --- a/app/models/style.rb +++ b/app/models/style.rb @@ -1,6 +1,6 @@ class Style < ActiveRecord::Base acts_as_paranoid - acts_as_list + acts_as_list :scope => :retailer_id has_many :style_categories, :order => 'position' has_many :product_styles, :class_name => "ProductStyle", @@ -8,10 +8,11 @@ class Style < ActiveRecord::Base has_many :product_styles, :class_name => "ProductStyle", :foreign_key => "style_id2" - validates_uniqueness_of :name, :message=>'は、既に使われています。' - def self.select_options(id = nil) + belongs_to :retailer + validates_uniqueness_of :name, :scope => :retailer_id, :message=>'は、既に使われています。' + def self.select_options(id = nil, retailer_id = 1) array = "" - find(:all).each{|s| + find(:all, :conditions => ["retailer_id = ? ", retailer_id], :order => "id").each{|s| if s.style_categories.size > 0 array += "