search for: classmethod

Displaying 20 results from an estimated 347 matches for "classmethod".

Did you mean: classmethods
2007 Jan 17
1
include ClassMethods in plugins
Why is that in so many plugins I see people using (including acts_as_taggable by DHH): # init.rb ActiveRecord::Base.send(:include, ActiveRecord::Acts::SomeModule) # lib/some_module.rb module ActiveRecord module Acts module SomeModule def self.included(base) base.extend(ClassMethods) end module ClassMethods def acts_as_something # code end end end end end why go through all that rather than just call: # init.rb ActiveRecord::Base.extend ActiveRecord::Acts::SomeModule::ClassMethods and skip the self.included method? --~--...
2005 May 14
2
ActiveRecord::Associations::ClassMethods Documentation Issue
I''m curious, but isn''t this example in the documentation for ActiveRecord::Associations::ClassMethods somewhat counter-intuitive? The SQL DDL is correct for the way it''s used in the models. But isn''t it more logical that an author would have many posts, rather than many authors having one post? From http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.ht ml...
2007 Nov 20
2
Plugin: create instance methods dynamically in ClassMethods?
Hi all I have the following plugin code: module IncenseCrud def self.included(base_class) base_class.extend(ClassMethods) end module ClassMethods def performs_incense_crud def index list return render(:action => ''list'') end end end end In init.rb I have the following: require File.dirname(__FILE__) + ''/lib/incense_crud'' ActionControl...
2007 Jun 27
5
Filter Ordering in Edge Rails (Rev 7143)
Filter chaining appears to behave in differently than Rails 1.2.x. It seems that I''m getting a lot of errors along the lines of: ActionController::ActionControllerError: filter #<ActionController::Filters::ClassMethods::BeforeFilterProxy:0x322f468 @filter=#<ActionController::Filters::ClassMethods::SymbolFilter: 0x322f4b8 @filter=:login_required>> was in the wrong place! This isn''t very helpful. How are you guys hunting down the problems with your filter ordering? James H. --~--~---------~-...
2008 Jan 13
2
module_eval and scope question
...framework, I found that I''m still lacking knowledge about scope issues in Ruby. Here is a stripped down version of the code I''m trying to understand (in case you happen to use Rails: This is from file scaffolding.rb): module ActionController module Scaffolding .. module ClassMethods def scaffold(model_id, options = {}) ... unless options[:suffix] module_eval <<-"end_eval", __FILE__, __LINE__ def index list end end_eval end ... end end end The purpose of th...
2006 May 17
3
Help mixin in class methods
Starting to feel more confident and code is DRYing up nicely, but I''ve hit a brick wall here and hoping someone can help me. I''ve got a module, TrustRanking, which extends a number of model''s instance methods, and I want to extend their class methods too, but I can seem to work out how to do it. Simply including the module from the class works a treat for the
2006 Jan 10
5
problems overriding module with plugin
...plug-in to fix the error in the rails core produced by the multiple delete on a HABTM relationship. I have confirmed that my plug-in is being included into the base during runtime however the code does not seem to be overridding the base class. module ActiveRecord module Associations module ClassMethods def has_and_belongs_to_many(association_id, options = {}, &extension) ............. code ............ end end end end this is what i have in my vendor/plugins/patches/lib directory, does anyone know why this is not working? Thanks, Mark -- -------...
2006 Jun 07
1
validates_presence_of in a mixed-in module?
Anyone got any idea of how to use built-in validation methods in a mixed-in module. What I''ve got at the moment is each class that needs ranking methods includes [the module] Ranking. Then the module Ranking has a bunch of instance methods and a submodule ClassMethods. The ClassMethods in this are mixed in to the class with self.included. So far so good. But I want the module to contain some validation definitions (using the built in methods validates_presence_of etc) for the classes that are mixin it in. Have tried putting them in the ClassMethods and the...
2005 Sep 11
3
Extending ActiveRecord::Base
Hey everyone, I''m interested in extending ActiveRecord::Base. I have a special "easier" find function that I want to include in all classes. How can I include this in all classes? Can I just create a model object that does something like this: class ExtendedAR < ActiveRecord::Base def self.special_find ... end end and then all my other classes
2006 Apr 18
4
Mixin variables
...Everything works fine, but I want to allow each controller to override the default login page with something like this: set_login_pages :secure => ''my_login'' In my module, I''ve set up the set_login_pages method like this: def self.included(base) base.extend(ClassMethods) end module ClassMethods def set_login_pages(*login_page_list) @login_pages = login_page_list end end But then when I try to access @login pages in the require_user method, it''s blank. What am I doing wrong? -- Posted via http://www.ruby-forum.com/.
2007 Sep 26
0
activerecord multiple databases
...#39;')) module ConnectionFactory class DB1 < ActiveRecord::Base self.establish_connection(Project::DBCONFIG[''db1'']) end class DB2 < ActiveRecord::Base self.establish_connection(Project::DBCONFIG[''db2'']) end end module ClassMethods module SomeClassName def long_query(name) r = find_by_sql ["SELECT ...",name] r.first unless r.empty? end end module SomeOtherClass def long_query(number) r = find_by_sql ["SELECT ...",name] r.first unless r.empty?...
2006 Apr 11
11
I can''t get rails to see my plugin. How can I this?
.../broom_stick/lib/broom_stick.rb Here are the contents of broom_stick.rb. It doesn''t add any new functionalit...yet. I wanted to make sure I could get the plugin seen by rails before I did any real coding. Thanks in advance for any assistance. module ActionController::Caching::Pages::ClassMethods # Expires the page that was cached with the +path+ as a key. Example: # expire_page "/lists/show" def expire_each_page(path) return unless perform_caching benchmaddrk "EXPIRED ALL PAGES: #{page_cache_file(path)}" do File.delete(page_cache_path(path)) if Fil...
2006 Feb 23
5
layouts
I created a controller called reports, for which I didn''t have any model or tables. I wanted to use a menu/layout structure similar to all my other models/controllers and added a layout named reports which then gives me the menu/layout structure but is also now part of all my reports. That isn''t gonna work so I renamed reports.rhtml to reports_forms.rhtml (still inside my
2006 Jun 03
1
Can I tell if the associated record is new in a belongs_to save?
In a belongs_to association, is there a way to tell if the associated object was newly created? Hopefully this will explain my question: A Firm class declares has_many<http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530>:clients and a client class declares belongs_to<http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000532>:firm. Now you do the following: Create a new firm and add clients using firm.clients.create methods. Note both firm and clients are unsave...
2012 Sep 28
0
Overriding class_attribute writers and order of super/extend C.M./included block eval in ActiveSupport::Concern
...ttribute is set via self.foobar = true before or after calling super to set the attribute. Unfortunately, I can''t find a clean way of doing this, and it is a little confusing when the order of things is typically to put included at the top even though it isn''t evaluated before ClassMethods is extended, etc. I''m not sure if it would help, but would be nice if the order of the following that happen in Concern''s def append_features(base) method were different and maybe then I could just override things in the ClassMethods module for class methods and the main bloc...
2007 Nov 27
1
Plugin - How to access object from instance method
Quick question: Given a basic plugin... how can I access an object that I instantiated in the acts_as method from an instance method? Probably some sort of accessor... but where? def self.included(base) base.extend ClassMethods end module ClassMethods def acts_as_test object = Object.new ## This object needs to be accessible from the Instance Methods end include Acts::As::Test::InstanceMethods end module InstanceMethods def do_something return object.process ## object created in acts_as_test end end -...
2009 Dec 27
5
Difficulties in understanding Rail-Plugins in depth
...lude to extend the functionality of a model. And exactly this is which i found in a file of the plugin. http://github.com/jackdempsey/acts_as_commentable/blob/master/lib/commentable_methods.rb I have found this part: module Juixe module Acts #:nodoc: module Commentable #:nodoc:       module ClassMethods         def acts_as_commentable           has_many :comments, :as => :commentable, :dependent => :destroy           include Juixe::Acts::Commentable::InstanceMethods           extend Juixe::Acts::Commentable::SingletonMethods         end       end Is this where the magic happens? I think s...
2006 Jun 26
5
How can I dynamiclly generate models?
...plugin, ''acts_as_commentable'', for my models - Image, Book, Music and so on, I do not want to use polymorphic association, so very model should have its own comment class. Here is my code acts_as_commentable.rb module Commentable def self.included(base) base.extend(ClassMethods) end module ClassMethods def acts_as_commentable(options = {}) class_def = "class Comment < ActiveRecord::Base set_table_name ''#{self.name.downcase}_comments'' belongs_to :user, :class_name=>''::User&...
2005 Dec 16
4
Validation with Aggregation
ActiveRecord supports composed_of for value objects which is fantastic but one thing that it doesn''t seem to support (or at least I am unable to find any documentation for) validation of the value objects. For example, given the following: class Message < ActiveRecord::Base composed_of :sender, :class_name => ''EmailAddress'' composed_of :recipient,
2006 Mar 13
1
adding custom cache field
...line, belongs_to_extra :accounts, :total_cache => :amount This code below is tested with edge rail, and will work for polymorphic associations also. Any comments are welcome. module ActiveRecord module Associations # :nodoc: def self.append_features(base) super base.extend(ClassMethods) end module ClassMethods def belongs_to_extra(association_id, options = { }) association_type = association_id.to_s + "_type" if association_id association_primary_key = association_id.to_s + "_id" if options[:total_cache] modul...