Created
August 19, 2010 00:33
-
-
Save dscataglini/536660 to your computer and use it in GitHub Desktop.
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 characters
# Usage include Categorizable::Categorized or Categorizable::Categorizable | |
module Categorizable | |
module Categorized | |
def self.included(klass) | |
associations = [ | |
[:has_many, :categorings], | |
[:has_many, :content_categories, {:through => :categorings}], | |
[:has_one, :primary_categoring, {:conditions => {:primary => true}, :class_name => "Categoring"}], | |
[:has_one, :primary_content_category, {:through => :primary_categoring}] | |
] | |
# showing off bad reflection | |
associations.each do |association| | |
klass.send( *association ) | |
end | |
klass.send(:include, ::Commons) | |
end | |
def content_category=(content_category) | |
Categoring.update_all("`primary` = 0", (self.class == FeedItem) ? "feed_item_id = #{id}" : "categorized_id = #{id} and categorized_type = '#{self.class}'") unless self.new_record? | |
options = {:categorized => self.publishable} | |
categoring = self.categorings(true).first(:conditions => { :content_category_id => content_category.id } ) || | |
self.publishable.categorings(true).first( :conditions => { :content_category_id => content_category.id } ) | |
if categoring | |
categoring.update_attribute(:primary, true) | |
else | |
method = new_record? ? :build : :create | |
self.categorings.send(method, options.merge(:content_category => content_category, :primary => true) ) | |
end | |
end | |
end | |
module Categorizable | |
def self.included(klass) | |
associations = [ | |
[:has_many, :categorings, {:as => :categorized, :dependent => :destroy}], | |
[:has_many, :content_categories, {:through => :categorings}], | |
[:has_one, :primary_categoring, {:as => :categorized, :conditions => {:primary => true}, :class_name => "Categoring"}], | |
[:has_one, :primary_content_category, {:through => :primary_categoring}] | |
] | |
# showing off bad reflection | |
associations.each do |association| | |
klass.send( *association ) | |
end | |
klass.send(:include, ::Commons) | |
end | |
def content_category=(content_category) | |
Categoring.update_all("`primary` = 0", (self.class == FeedItem) ? "feed_item_id = #{id}" : "categorized_id = #{id} and categorized_type = '#{self.class}'") unless self.new_record? | |
options = self.class == StockPosition ? {:feed_item => self.feed_items.last} : {:feed_item => self.feed_item} | |
categoring = self.categorings(true).first( :conditions => { :content_category_id => content_category.id } ) | |
if categoring | |
categoring.update_attribute(:primary, true) | |
else | |
method = new_record? ? :build : :create | |
self.categorings.send(method, options.merge(:content_category => content_category, :primary => true) ) | |
end | |
end | |
end | |
module Common | |
def content_category_id=( category_id ) | |
self.content_category = ContentCategory.find( category_id ) if category_id | |
end | |
def content_category_id | |
self.content_category.id if self.content_category | |
end | |
def content_category(reload = false) | |
return @_content_category if @_content_category && !reload | |
if primaries = primary_content_category(reload) | |
@_content_category = primaries.all.first | |
else | |
@_content_category = content_categories(reload).first | |
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 characters
class NilClass | |
def method_missing(name, *args, &proc) | |
nil | |
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 characters
class Research < ActiveRecord::Base | |
# just imagine what it used to look | |
(1..8).each do |num| | |
has_one "chart#{num}".to_sym, :class_name => "Image", :conditions => {:identifier => "Chart#{num}"}, :dependent => :destroy, :as => :attachable | |
end | |
# This was actually in the controller | |
(1..8).each do |num| | |
define_method "chart#{num}=".to_sym do |params| | |
if(params && !params[:uploaded_data].blank?) | |
if (send("chart#{num}")) | |
send("chart#{num}").update_attributes( params ) | |
else | |
send("build_chart#{num}", {:identifier => "Chart#{num}"}.merge( params || {} ) ) | |
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 characters
module WrongCategorizable | |
def self.included(klass) | |
associations = { | |
:feed_item => [ | |
[:has_many, :categorings], | |
[:has_many, :content_categories, {:through => :categorings}], | |
[:has_one, :primary_categoring, {:conditions => {:primary => true}, :class_name => "Categoring"}], | |
[:has_one, :primary_content_category, {:through => :primary_categoring}] | |
], | |
:categorized => [ | |
[:has_many, :categorings, {:as => :categorized, :dependent => :destroy}], | |
[:has_many, :content_categories, {:through => :categorings}], | |
[:has_one, :primary_categoring, {:as => :categorized, :conditions => {:primary => true}, :class_name => "Categoring"}], | |
[:has_one, :primary_content_category, {:through => :primary_categoring}] | |
]} | |
# showing off bad reflection | |
associations[(klass == FeedItem) ? :feed_item : :categorized].each do |association| | |
klass.send( *association ) | |
end | |
end | |
def content_category_id=( category_id ) | |
self.content_category = ContentCategory.find( category_id ) if category_id | |
end | |
def content_category_id | |
self.content_category.id if self.content_category | |
end | |
def content_category(reload = false) | |
return @_content_category if @_content_category && !reload | |
if primaries = primary_content_category(reload) | |
@_content_category = primaries.all.first | |
else | |
@_content_category = content_categories(reload).first | |
end | |
end | |
def content_category=(content_category) | |
Categoring.update_all("`primary` = 0", (self.class == FeedItem) ? "feed_item_id = #{id}" : "categorized_id = #{id} and categorized_type = '#{self.class}'") unless self.new_record? | |
if self.class == FeedItem | |
options = {:categorized => self.publishable} | |
categoring = self.categorings(true).first(:conditions => { :content_category_id => content_category.id } ) || | |
self.publishable.categorings(true).first( :conditions => { :content_category_id => content_category.id } ) | |
else | |
options = self.class == StockPosition ? {:feed_item => self.feed_items.last} : {:feed_item => self.feed_item} | |
categoring = self.categorings(true).first( :conditions => { :content_category_id => content_category.id } ) | |
end | |
if categoring | |
categoring.update_attribute(:primary, true) | |
else | |
method = new_record? ? :build : :create | |
self.categorings.send(method, options.merge(:content_category => content_category, :primary => true) ) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment