Created
February 2, 2016 22:28
-
-
Save jbender/501b4e16e4edda5493e6 to your computer and use it in GitHub Desktop.
Example RubyMotion Network Model
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 ContactuallyApi | |
module Models | |
class Contact < Base | |
include NameHelper | |
ATTRIBUTES = CORE_ATTRIBUTES + [ | |
:addresses, | |
:avatar_url, | |
:company, | |
:email_addresses, | |
:first_name, | |
:followup_source, | |
:last_contacted, | |
:last_name, | |
:phone_numbers, | |
:relationship_status, | |
:social_media_profiles, | |
:title, | |
:websites | |
] | |
attr_accessor(*ATTRIBUTES) | |
# Associations | |
belongs_to :followup_source, :class_name => 'Bucket' | |
has_many :addresses, :class_name => 'ContactAddress' | |
has_many :buckets, :class_name => 'ContactBucket' | |
has_many :email_addresses, :class_name => 'ContactEmailAddress' | |
has_many :phone_numbers, :class_name => 'ContactPhoneNumber' | |
has_many :social_media_profiles, | |
:class_name => 'ContactSocialMediaProfile' | |
has_many :websites, :class_name => 'ContactWebsite' | |
# Network/Persistence | |
def self.index(params = nil, &callback) | |
fail 'remote calls require callbacks' unless block_given? | |
ContactuallyApi.contact_index(params) do |response, error| | |
contacts = new_object_array_or_nil(response) | |
callback.call(contacts, error) | |
end | |
end | |
def destroy!(&callback) | |
fail 'remote calls require callbacks' unless block_given? | |
ContactuallyApi.contact_destroy( | |
id, &error_or_assign_attributes_from_server(&callback) | |
) | |
end | |
def fetch(&callback) | |
ContactuallyApi.contact_show( | |
id, &error_or_assign_attributes_from_server(&callback) | |
) | |
end | |
def update_buckets(given_buckets, &callback) | |
arguments = [ | |
id, | |
given_buckets.map { |bucket| { :id => bucket.id } } | |
] | |
ContactBucket.update(*arguments) do |buckets, error| | |
self.buckets = buckets if buckets | |
callback.call(buckets, error) | |
end | |
end | |
# Custom Setters | |
def last_contacted=(last_contacted_data) | |
return unless last_contacted_data | |
@last_contacted = Time.iso8601(last_contacted_data) | |
end | |
# View Helpers | |
def email | |
return unless email_addresses.present? | |
email_addresses.first.address | |
end | |
def phone | |
return unless phone_numbers.present? | |
phone_numbers.first.number | |
end | |
def title_at_company | |
[title, company].select(&:present?).join(' at ') | |
end | |
private | |
def method_name_for_save | |
new_record? ? :contact_create : :contact_update | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment