Browse Source

Merge pull request #72 from spacemunkay/sorting-and-shop-id-fix-68-63

Sorting and shop id fix 68 63
topic-sass
Jason Denney 11 years ago
parent
commit
f17c168d1d
  1. 4
      app/components/bikes.rb
  2. 4
      app/models/bike.rb
  3. 23
      db/migrate/20131019023429_change_bike_shop_id.rb
  4. 6
      db/schema.rb
  5. 4
      db/seeds.rb
  6. 4
      spec/factories/bikes.rb

4
app/components/bikes.rb

@ -33,6 +33,10 @@ class Bikes < Netzke::Basepack::Grid
}
}
]
# Default the sorting to ASC on shop_id
c.data_store.sorters = [{ property: 'shop_id', direction: 'ASC' }]
@bike = Bike.all
c.prohibit_update = true if cannot? :update, @bike
c.prohibit_create = true if cannot? :create, @bike

4
app/models/bike.rb

@ -12,9 +12,7 @@ class Bike < ActiveRecord::Base
belongs_to :bike_condition
belongs_to :bike_purpose
default_scope order('shop_id ASC')
validates :shop_id, :presence => true, :uniqueness => true, :length => { :minimum => 3 }
validates :shop_id, :presence => true, :uniqueness => true, :numericality => { :only_integer => true }
validates :serial_number, :length => { :minimum => 3 }
validates :model, :length => { :maximum => 50 }
validates :bike_brand_id, :presence => true

23
db/migrate/20131019023429_change_bike_shop_id.rb

@ -0,0 +1,23 @@
class ChangeBikeShopId < ActiveRecord::Migration
# We don't use change_column because we have arbitrary strings we
# need to strip the characters from to make the integer values
def up
bikes = Bike.all
remove_column :bikes, :shop_id
add_column :bikes, :shop_id, :integer, :unique => true
# deconflict shop ids
bikes.each do |bike|
new_id = bike.shop_id.gsub(/[^\d]/, '').to_i rescue 1
while bikes.map(&:shop_id).include? new_id
new_id += 1
end
bike.shop_id = new_id
end
bikes.each(&:save)
end
def down
change_column :bikes, :shop_id, :string
end
end

6
db/schema.rb

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130902201853) do
ActiveRecord::Schema.define(:version => 20131019023429) do
create_table "bike_actions", :force => true do |t|
t.string "action", :limit => 128, :null => false
@ -60,12 +60,10 @@ ActiveRecord::Schema.define(:version => 20130902201853) do
t.integer "bike_purpose_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "shop_id"
t.string "model"
t.integer "shop_id"
end
add_index "bikes", ["shop_id"], :name => "index_bikes_on_shop_id", :unique => true
create_table "credit_conversions", :force => true do |t|
t.integer "conversion", :default => 1
t.datetime "created_at", :null => false

4
db/seeds.rb

@ -16,8 +16,10 @@ end
if BikeBrand.all.empty? and BikeModel.all.empty?
# Need to use DEFAULT instead of explicit IDs that are used in the sql file,
# so that the PG table ID sequence is incremented
#
# Note the drop(1) which assumes we have a junk PRAGMA line at the top
load_statements = File.readlines(File.join(Rails.root, 'db', 'seed', 'sql', 'bike_brands_and_models.sql')).drop(1).map do |statement|
statement.sub(/VALUES\(\d+,/, 'VALUES(DEFAULT,').tap {|x| puts x }
statement.sub(/VALUES\(\d+,/, 'VALUES(DEFAULT,')
end
ActiveRecord::Base.connection.execute(load_statements.join)
end

4
spec/factories/bikes.rb

@ -2,9 +2,7 @@
FactoryGirl.define do
factory :bike do
sequence :shop_id do |n|
"Shop ID #{n}"
end
sequence(:shop_id) {|n| n}
sequence :serial_number do |n|
"S/N# #{n}"
end

Loading…
Cancel
Save