lib/doeskeyvalue/indexes.rb in doeskeyvalue-0.1.1 vs lib/doeskeyvalue/indexes.rb in doeskeyvalue-0.1.2
- old
+ new
@@ -11,11 +11,11 @@
raise DoesKeyValue::NoColumnNameSpecified unless key_value_column
raise DoesKeyValue::NoKeyNameSpecified unless key_name
raise DoesKeyValue::KeyAndIndexOptionsMustBeHash unless opts.is_a?(Hash)
search_key = "#{key_value_column}.#{key_name}"
- raise DoesKeyValue::NoKeyForThatIndex if !self.respond_to?(key_name) || !self.respond_to?("#{key_name}=")
+ # TODO: raise DoesKeyValue::NoKeyForThatIndex if !self.respond_to?(key_name) || !self.respond_to?("#{key_name}=")
class_name = self.name.underscore
class_table_name = self.table_name
index_table_name = "key_value_index"
@@ -59,20 +59,37 @@
# Provide a callback after save which updates the index
define_method("update_index_#{key_value_column}_#{key_name}_after_save") do
class_name = self.class.name.underscore
class_table_name = self.class.table_name
- index_table_name = "key_value_indexes"
+ index_table_name = "key_value_index"
+
# TODO: Restrict value to 255 characters, the table-enforced limit
- idx_id = ActiveRecord::Base.connection.insert("INSERT INTO `#{index_table_name}` (`obj_type`,`obj_id`,`key_name`,`value`) VALUES (\""+self.class.to_s+"\","+self.id.to_s+", \""+search_key.to_s+"\", \"#{self.send(key_name).to_s}\")")
+
+ # TODO: Serialize value in such a nils to be properly represented
+ # Sample JSON Serialization: tab.to_json
+ # Sample JSON Reconstruction: Tab.new( JSON.parse(tab.to_json)["tab"] )
+
+ new_value = self.send(key_name)
+ up_count = ActiveRecord::Base.connection.update("
+ UPDATE `#{index_table_name}` SET `value` = \"#{new_value}\", `updated_at` = NOW()
+ WHERE `obj_type`=\"#{self.class}\" AND `obj_id`=#{self.id} AND `key_name`=\"#{search_key}\"
+ ")
+ if !new_value.nil? && up_count == 0
+ idx_id = ActiveRecord::Base.connection.insert(
+ "INSERT INTO `#{index_table_name}` (`obj_type`,`obj_id`,`key_name`,`value`,`created_at`,`updated_at`) VALUES (\"#{self.class}\", #{self.id}, \"#{search_key}\", \"#{new_value}\", NOW(), NOW())
+ ")
+ return idx_id
+ end
+
end
after_save "update_index_#{key_value_column}_#{key_name}_after_save"
# Provide a callback after destroy to likewise update the index
define_method("update_index_#{key_value_column}_#{key_name}_after_destroy") do
class_name = self.class.name.underscore
class_table_name = self.class.table_name
- index_table_name = "key_value_indexes"
+ index_table_name = "key_value_index"
num_del = ActiveRecord::Base.connection.delete("DELETE FROM `#{index_table_name}` WHERE `obj_type` = \"#{self.class}\" AND `obj_id` = #{self.id}")
end
after_destroy "update_index_#{key_value_column}_#{key_name}_after_destroy"
end
\ No newline at end of file