Skip to content

Instantly share code, notes, and snippets.

@jamilbk
Created October 4, 2011 03:30
Show Gist options
  • Save jamilbk/1260841 to your computer and use it in GitHub Desktop.
Save jamilbk/1260841 to your computer and use it in GitHub Desktop.
#### image_map_coordinate.rb ####
class ImageMapCoordinate < ActiveRecord::Base
belongs_to :image_map
# FIXME: validation bug?
%w(w h x y).each do |v|
validates_numericality_of eval ":#{v}"
end
validate :href_validator
private
def href_validator
# regex URL validator
if (self.href =~ URI::regexp).nil?
errors.add(:href, "must be a valid URL")
end
end
end
#### image_map.rb ####
class ImageMap < ActiveRecord::Base
belongs_to :plugin
belongs_to :brand
has_one :image_map_image, :dependent => :destroy
has_many :image_map_coordinates, :dependent => :destroy
# TODO: Dry this up!
validates_numericality_of :render_priority
# enforce has_one relationships
validates :brand_id, :uniqueness => true
# ensure brand_id and plugin_id point to existing objects
validates_presence_of :plugin
validates_presence_of :brand
end
#### image_maps_controller.rb ####
...
def update
@image_map = ImageMap.find(params[:id])
coords = []
params[:coords].each do |c|
coords << ImageMapCoordinate.new(c)
end
@image_map.image_map_coordinates = coords
respond_to do |format|
if @image_map.update_attributes(params[:image_map])
format.html { redirect_to edit_brand_url(@image_map.brand), notice: 'plugin was successfully updated.' }
else
format.html { render :template => 'plugins/image_map/edit' }
end
end
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment