Created
April 26, 2013 17:28
Revisions
-
bradly created this gist
Apr 26, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ module ActiveRecordSingleton def self.included(base) base.class_eval do class << self delegate :attributes, :save, :save!, :update_attribute, :update_attributes, :update_attributes!, :update_column, to: 'instance' [:new, :create, :create!, :destroy, :destroy_all, :delete, :delete_all].each do |method_name| undef_method method_name end def instance @@singleton ||= self.first_or_create! end end [:destroy, :delete, :clone, :dup].each do |method_name| undef_method method_name end column_names.each do |column_name| define_singleton_method column_name.to_sym do instance.send(column_name.to_sym) end define_singleton_method :"#{column_name}=" do |value| instance.send(:"#{column_name}=", value) end end end end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ class Settings < ActiveRecord::Base include ActiveRecordSingleton end