daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Feb 11th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Theme < Base
  2.  
  3.   include ActiveModel::Validations
  4.  
  5.   attr_accessor :id, :name
  6.   validate      :uniqueness_of_theme_name, :on => :create
  7.  
  8.   def initialize(attrs = {})
  9.     attrs.each do |name, value|
  10.       send("#{name}=", value)
  11.     end
  12.   end
  13.  
  14.   def uniqueness_of_theme_name
  15.     errors.add(:name, "name is already in use") unless Theme.find_by_name(name).nil?
  16.   end
  17.  
  18.   def self.find_by_name(name)
  19.     return store.find({ 'name' => "/^#{name}$/i" }).any?
  20.   end
  21.  
  22.   def self.create(attributes = {})
  23.     theme = Theme.new(attributes)
  24.     theme.save
  25.  
  26.     return theme
  27.   end
  28.  
  29.   def persisted?
  30.     id && !id.nil?
  31.   end
  32.  
  33.   def save()
  34.     # only call validation during create context
  35.     context = persisted? :update : :create
  36.     false if invalid?(context)
  37.     store.save(self)
  38.     true
  39.   end
  40.  
  41.   def self.create(attrs = {})
  42.     theme = Theme.new(attrs)
  43.     theme.save
  44.     theme
  45.   end
  46.  
  47. end
  48.    
  49. class Theme
  50.  
  51.   include ActiveModel::Validations
  52.   include ActiveModel::Conversion
  53.   extend ActiveModel::Naming
  54.  
  55.   attr_accessor :id, :name
  56.  
  57.   validates :name, uniqueness: true, on: :create
  58.  
  59.   def initialize(attributes = {})
  60.     attributes.each do |name, value|
  61.       send("#{name}=", value)
  62.     end
  63.   end
  64.  
  65.   def persisted?
  66.     false
  67.   end
  68. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top