Skip to content

Instantly share code, notes, and snippets.

View Ross-Hunter's full-sized avatar

Ross-Hunter Ross-Hunter

View GitHub Profile
#
# The goal of this exercise is work on identifying abstraction which helps simplify, document,
# and separate the concerns going on in file.
#
# Exercise:
# * Find related ideas in the below code
# * Abstract them out (methods, modules, classes, etc, you pick!)
# * If you find multiple ways, then do a separate gist for each way.
# * Rinse repeat until you see no other ways.
#
gotoExternalApp = function () {
var options = Environment.appOptions;
console.log ("Launching " + options.appName);
ExternalApp.launch(options);
};
gotoExternalApp();
"appOptions": {
"appStoreUrl":"https://itunes.apple.com/us/app/app_name/idXXX",
"iosUrlScheme":"app_name://launchedfromthisappwiththeseoptions",
"androidAppID":"com.XXX.app_name",
"appName":"App Name"
}
## Contrived Example
# Will the thing float?
class Thing
attr_accessor :mass, :volume
def will_i_float?
(mass / volume) < 1
end
##Class
class User
belongs_to :group, inverse_of: :member
end
class Group
# inverse_of because we are adding options to the relationship that prevent
# automatic guessing of the inverse
has_many :members, class_name: "User", inverse_of: :user
##Class
class User
has_one :profile # inverse_of :profile not necessary in 4.1+
end
class Profile
belongs_to :user # inverse_of :user not necessary in 4.1+
end
##Class
class User
has_one :profile
has_one :avatar, through: :profile
end
class Profile
belongs_to :user
has_one :avatar
##Class
class User
has_one :profile
has_many :avatars, through: :profile
end
class Profile
belongs_to :user
has_many :avatars
##Class
class User
has_and_belongs_to_many :groups
end
class Group
has_and_belongs_to_many :groups
end
##Class
class User
has_many :memberships
has_many :groups, through: :memberships
end
class Membership
belongs_to :user
belongs_to :group