Skip to content

Instantly share code, notes, and snippets.

@adkron
Last active December 15, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adkron/5214482 to your computer and use it in GitHub Desktop.
Save adkron/5214482 to your computer and use it in GitHub Desktop.
module ApiEndpoint
def endpoint_for(id)
id.constantize.eval do
has_many self.underscore, :through => :api_objects, :source => :endpoint, :source_type => self.to_s
end
has_many :api_objects, :as => :endpoint
has_many id, :through => :api_objects
end
end
class ApiObject < ActiveRecord::Base
belongs_to :endpoint, :polymorphic => true
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :api_objects
end
class GetGlueObject < ActiveRecord::Base
extend ApiEndpoint
endpoint_for :properties
end
class InstagramTag < ActiveRecord::Base
extend ApiEndpoint
endpoint_for :properties
end
class InstagramLocation < ActiveRecord::Base
extend ApiEndpoint
endpoint_for :properties
end

Currently we have 3 types of ApiObjects. There will be more in the future.

  • InstagramTags
  • InstagramLocations
  • GetGlueObjects

Properties should have many API Objects and API Objects should have many Properties. (i.e. an InstagramTag of "#sxsw" could belong to multiple Properties. But, a Property could have "#sxsw", "#oblivionsxsw", and "#sxswmusic" as its InstagramTags.)

We want to avoid having to have a join table for each type of ApiObjects.

Each type of API Objects needs to be its own table with its own attributes and a Property needs to be able to call on all of its API Object by type. Property.get_glue_objects, for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment