Skip to content

Instantly share code, notes, and snippets.

@dirkkelly
Created December 20, 2010 06:27
Show Gist options
  • Save dirkkelly/748088 to your computer and use it in GitHub Desktop.
Save dirkkelly/748088 to your computer and use it in GitHub Desktop.
overrides the page method missing to attempt to find or update a field
module Shop
module Models
module Page
def self.included(base)
base.class_eval do
# Returns the field with the name passed.
# If a second parameter is passed the field content will be updated
def field(name,*args)
if new_record? or fields.any?(&:new_record?)
field = fields.detect { |f| f.name.downcase == name.to_s.downcase }
elsif !args.empty?
field = fields.find_or_create_by_name name.to_s
field.update_attribute(:content, args[0].to_s)
else
field = fields.find_by_name name.to_s
end
field
end
def method_missing(method,*args,&block)
begin
field(method.to_s.gsub("=",""),*args)
rescue
super(method,*args,&block)
end
end
def allowed_children_with_shop
allowed_children_without_shop.reject { |p| p.name =~ /(Product|Order|LineItem)/ }
end
alias_method_chain :allowed_children, :shop
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment