Skip to content

Instantly share code, notes, and snippets.

@pda
Created November 30, 2011 10:58
Show Gist options
  • Save pda/1408675 to your computer and use it in GitHub Desktop.
Save pda/1408675 to your computer and use it in GitHub Desktop.
Example of class @instance_var that used to be a @@class_var
module Category::Categorizable
extend ActiveSupport::Concern
included do
def self.categorizable_through(table)
has_many table, dependent: :destroy
@categorizable_join_model = Kernel.const_get(table.to_s.singularize.classify)
end
def categories
Category::CategoriesRelation.new(
self,
self.class.instance_variable_get(:@categorizable_join_model)
)
end
end
end
module Category::Categorizable
extend ActiveSupport::Concern
included do
def self.categorizable_through(table)
@@categorizable_join_model = Kernel.const_get(table.to_s.singularize.classify)
end
def categories
Category::CategoriesRelation.new(self, @@categorizable_join_model)
end
end
end
class Post < ActiveRecord::Base
include Category::Categorizable
categorizable_through :categories_posts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment