Skip to content

Instantly share code, notes, and snippets.

@davinmsu
Created April 8, 2014 21:35
Show Gist options
  • Save davinmsu/10196384 to your computer and use it in GitHub Desktop.
Save davinmsu/10196384 to your computer and use it in GitHub Desktop.
class Setting < ActiveRecord::Base
enum type: {integer: 0, string: 1, storage: 2}
enum_features :type
store :value, coder: JSON
after_find :match_types
before_save :save_types
validates :value, numericality: { only_integer: true }, if: 'integer?'
validates :title, uniqueness: true
def self.get(title)
find_by(title: title)
end
{integer: '', string: '', storage: {}}
private
def match_types
self.value = value[self.type.to_sym]
end
def save_types
self.value = {integer: self.value.to_i, string: nil, storage: nil} if integer?
self.value = {integer: nil, string: self.value.to_s, storage: nil} if string?
self.value = {integer: nil, string: nil, storage: self.value} if storage? #todo check if its possible
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment