GithubHelp home page GithubHelp logo

acts_as_singleton's Introduction

acts_as_singleton

github.com/stephencelis/acts_as_singleton

DESCRIPTION

A lightweight singleton library for your Active Record models.

It just makes sense to store mutable, site-wide, admin-level settings in the database. Right? A key-value table may be more flexible, but maybe we don’t want to be flexible!

If you truly want that flexibility: github.com/stephencelis/kvc

FEATURES/PROBLEMS

  • Lightning-fast queries! Doesn’t get any faster than this! (Guaranteed.)

  • Follows the ultra-cool “singleton” pattern! (Tell your friends.)

  • What? That’s not the “cool” singleton in Ruby? (Don’t tell your friends?)

SYNOPSIS

class HomepageSettings < ActiveRecord::Base
  include ActiveRecord::Singleton
end

How Rubyish! Oh, you want it Railsish? Very well…

class HomepageSettings < ActiveRecord::Base
  acts_as_singleton
end

Have your cake and eat your silly idiom, too! Just try to create a row!

HomepageSettings.instance # => #<HomepageSettings...>

Don’t even try to access it otherwise. It won’t work!

REQUIREMENTS

  • Active Record 2.3.2 or greater.

  • A delicate palate.

INSTALL

As a gem

Configure:

# config/environment.rb
config.gem "acts_as_singleton"

And install:

% [sudo] rake gems:install

As a plugin

Traditional:

% script/plugin install git://github.com/stephencelis/acts_as_singleton.git

Or, as a submodule:

% git submodule add git://github.com/stephencelis/acts_as_singleton.git \
                    vendor/plugins/acts_as_singleton

LICENSE

(The MIT License)

© 2009-2012 Stephen Celis, [email protected].

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

acts_as_singleton's People

Contributors

aaron avatar crmaxx avatar dush avatar logicaltext avatar sameertotey avatar stephencelis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

acts_as_singleton's Issues

SubclassNotFound when using acts_as_singleton with ActiveRecord inheritance

Hi,

I have the following setup:

class VirtualPlaylist < ActiveRecord::Base; acts_as_singleton; end
class MyFavorites < VirtualPlaylist; end
class RecentlyPlayedSongs < VirtualPlaylist; end

Accessing the singletons fails in this case with the following error:

musicsocial> VirtualPlaylist.instance
=> #<VirtualPlaylist name: nil, type: nil>
musicsocial> MyFavorites.instance
=> #<MyFavorites name: nil, type: "MyFavorites">
musicsocial> MyFavorites.instance
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'MyFavorites'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite MyFavorites.inheritance_column to use another column for that information.
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1637:in `instantiate_without_polymorphic_checks'
    from /Users/karl/projects/musicsocial/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/base.rb:52:in `instantiate'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `collect!'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1548:in `find_every'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1505:in `find_initial'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:613:in `find'
    from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:623:in `first'
    from /Users/karl/projects/musicsocial/vendor/plugins/acts_as_singleton/lib/acts_as_singleton.rb:42:in `instance'
    from (irb):4

However if I comment out the line in acts_as_singleton.rb which privatizes a bunch of the instance methods, it works.

#private_class_method *methods.grep(PRIVATE) # Deny existent others.

musicsocial> VirtualPlaylist.instance
=> #<VirtualPlaylist name: nil, type: nil>
musicsocial> MyFavorites.instance
=> #<MyFavorites name: nil, type: "MyFavorites">
musicsocial> RecentlyPlayedSongs.instance
=> #<RecentlyPlayedSongs name: nil, type: "RecentlyPlayedSongs">

I'm not sure which method it is that is causing the problem.

Regards,
Karl

This gem doesn't play fine with rails 3.2

When invoking ::instance

ArgumentError: wrong number of arguments (2 for 1)
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in find_by_sql' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:inexec_queries'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:159:in to_a' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/explain.rb:40:inlogging_query_plan'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:158:in to_a' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:377:infind_first'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:122:in first' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:insend'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in first' from /.rvm/gems/ruby-1.8.7-p357/gems/acts_as_singleton-0.0.5/lib/acts_as_singleton.rb:43:ininstance'

rails 3 compatibility issues with find_by_sql again

Given this simple model

class Platform < ActiveRecord::Base
  acts_as_singleton
end

and using 3.1.0.rc4 I get the following error when doing a

 Platform.instance

in the rails console:

>> Platform.instance
ArgumentError: wrong number of arguments (2 for 1)
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/bundler/gems/acts_as_singleton-4229bec5929a/lib/acts_as_singleton.rb:58:in `find_by_sql'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:109:in `to_a'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:370:in `find_first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:444:in `first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/bundler/gems/acts_as_singleton-4229bec5929a/lib/acts_as_singleton.rb:43:in `instance'
from (irb):1
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>

Even when using the most recent version via adding this line

gem 'acts_as_singleton',  :git => 'https://github.com/stephencelis/acts_as_singleton.git'

to my Gemfile.

It seems to be the same issue as here: #3

Any ideas?

rails 3 compatibility

The code below is running perfectly well with rails 2.3.5 but not with rails 3.0.0.4beta:

class FeaturedMember < ActiveRecord::Base
acts_as_singleton
belongs_to :user
end

( $ ) rails c
Loading development environment (Rails 3.0.0.beta4)
irb(main):001:0> FeaturedMember.instance
NoMethodError: private method find_by_sql' called for #<Class:0xb5bde13c> from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1016:inmethod_missing' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation.rb:64:in to_a' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:324:infind_first' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:117:in first' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:insend' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in first' from /usr/lib/ruby/gems/1.8/gems/acts_as_singleton-0.0.4/lib/acts_as_singleton.rb:43:ininstance' from (irb):1
irb(main):002:0>

rails 3.1 is out

...and bundle update rails fails with:

bundle update rails
Fetching source index for http://rubygems.org/
Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    railties (~> 3.1.0.rc1) depends on
      activesupport (= 3.1.0.rc1)

    acts_as_singleton (= 0.0.6.beta) depends on
      activesupport (3.1.0.rc4)

Possible thread-safety issue

the instance method should be

def instance
  self.transaction { first || create }
end

thereby preventing more than one record from being create'ed

Form Helpers

Trying to make a form with a singleton model using form_for doesn't really work. It'd be nice if it did work.

Rails 4.1.10 & acts_as_singleton 0.0.8 - NoMethodError: private method `find_generated_attribute_method'

Rails 4.1.10 & acts_as_singleton 0.0.8

I'm getting the following error when calling an instance:

rails c test
Loading test environment (Rails 4.1.10)
irb: warn: can't alias context from irb_context.
2.1.5 :001 > GlobalSetting.instance
  GlobalSetting Load (2.8ms)  SELECT  "global_settings".* FROM "global_settings"   ORDER BY "global_settings"."id" ASC LIMIT 1
   (1.1ms)  BEGIN
   (0.3ms)  ROLLBACK
NoMethodError: private method `find_generated_attribute_method' called for #<Class:0x007fc79903bea8>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.