From 0dc3cc0d00b8dc96460d0ee1ad4ab238dc5c766e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 24 Jan 2013 09:18:29 +0100 Subject: [PATCH] Small refactoring. --- app/models/store.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/models/store.rb b/app/models/store.rb index a7a75108f..0c9c5d050 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -1,6 +1,6 @@ require 'digest/md5' -class Store < ActiveRecord::Base +class Store < ApplicationModel store :preferences belongs_to :store_object, :class_name => 'Store::Object' belongs_to :store_file, :class_name => 'Store::File' @@ -10,12 +10,7 @@ class Store < ActiveRecord::Base data = data.stringify_keys # lookup store_object.id - store_object = Store::Object.where( :name => data['object'] ).first - if !store_object || !store_object.id - store_object = Store::Object.create( - :name => data['object'] - ) - end + store_object = Store::Object.create_if_not_exists( :name => data['object'] ) data['store_object_id'] = store_object.id # check if record already exists @@ -26,7 +21,7 @@ class Store < ActiveRecord::Base # check real store md5 = Digest::MD5.hexdigest( data['data'] ) - data['size'] = data['data'].to_s.to_blob.bytesize + data['size'] = data['data'].to_s.bytesize file = Store::File.where( :md5 => md5 ).first @@ -69,11 +64,11 @@ class Store < ActiveRecord::Base return true end - class Object < ActiveRecord::Base + class Object < ApplicationModel validates :name, :presence => true end - class File < ActiveRecord::Base + class File < ApplicationModel before_validation :add_md5 private