Last active
December 15, 2015 23:49
-
-
Save lankz/5343229 to your computer and use it in GitHub Desktop.
Examples of monster functions from various Ruby codebases...
This file contains 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
# length, structure of these methods make it difficult to mess with while preserving existing functionality | |
def const_missing(const_name, nesting = nil) | |
klass_name = name.presence || "Object" | |
unless nesting | |
# We'll assume that the nesting of Foo::Bar is ["Foo::Bar", "Foo"] | |
# even though it might not be, such as in the case of | |
# class Foo::Bar; Baz; end | |
nesting = [] | |
klass_name.to_s.scan(/::|$/) { nesting.unshift $` } | |
end | |
# If there are multiple levels of nesting to search under, the top | |
# level is the one we want to report as the lookup fail. | |
error = nil | |
nesting.each do |namespace| | |
begin | |
return Dependencies.load_missing_constant Inflector.constantize(namespace), const_name | |
rescue NoMethodError then raise | |
rescue NameError => e | |
error ||= e | |
end | |
end | |
# Raise the first error for this set. If this const_missing came from an | |
# earlier const_missing, this will result in the real error bubbling | |
# all the way up | |
raise error | |
end | |
def load_missing_constant(from_mod, const_name) | |
log_call from_mod, const_name | |
unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod) | |
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" | |
end | |
raise NameError, "#{from_mod} is not missing constant #{const_name}!" if local_const_defined?(from_mod, const_name) | |
qualified_name = qualified_name_for from_mod, const_name | |
path_suffix = qualified_name.underscore | |
file_path = search_for_file(path_suffix) | |
if file_path && ! loaded.include?(File.expand_path(file_path).sub(/\.rb\z/, '')) # We found a matching file to load | |
require_or_load file_path | |
raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless local_const_defined?(from_mod, const_name) | |
return from_mod.const_get(const_name) | |
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) | |
return mod | |
elsif (parent = from_mod.parent) && parent != from_mod && | |
! from_mod.parents.any? { |p| local_const_defined?(p, const_name) } | |
# If our parents do not have a constant named +const_name+ then we are free | |
# to attempt to load upwards. If they do have such a constant, then this | |
# const_missing must be due to from_mod::const_name, which should not | |
# return constants from from_mod's parents. | |
begin | |
return parent.const_missing(const_name) | |
rescue NameError => e | |
raise unless e.missing_name? qualified_name_for(parent, const_name) | |
end | |
end | |
raise NameError, | |
"uninitialized constant #{qualified_name}", | |
caller.reject {|l| l.starts_with? __FILE__ } | |
end |
This file contains 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
def create | |
if params[:password_confirmation] != honeypot_value || params[:challenge] != challenge_value.try(:reverse) | |
# Don't give any indication that we caught you in the honeypot | |
return render(json: {success: true, active: false, message: I18n.t("login.activate_email", email: params[:email]) }) | |
end | |
user = User.new | |
user.name = params[:name] | |
user.email = params[:email] | |
user.password = params[:password] | |
user.username = params[:username] | |
auth = session[:authentication] | |
if auth && auth[:email] == params[:email] && auth[:email_valid] | |
user.active = true | |
end | |
user.password_required! unless auth | |
DiscourseHub.register_nickname( user.username, user.email ) if user.valid? && SiteSetting.call_discourse_hub? | |
if user.save | |
msg = nil | |
active_result = user.active? | |
if active_result | |
# If the user is active (remote authorized email) | |
if SiteSetting.must_approve_users? | |
msg = I18n.t("login.wait_approval") | |
active_result = false | |
else | |
log_on_user(user) | |
user.enqueue_welcome_message('welcome_user') | |
msg = I18n.t("login.active") | |
end | |
else | |
msg = I18n.t("login.activate_email", email: user.email) | |
Jobs.enqueue(:user_email, type: :signup, user_id: user.id, email_token: user.email_tokens.first.token) | |
end | |
# Create auth records | |
if auth.present? | |
if auth[:twitter_user_id] && auth[:twitter_screen_name] && TwitterUserInfo.find_by_twitter_user_id(auth[:twitter_user_id]).nil? | |
TwitterUserInfo.create(user_id: user.id, screen_name: auth[:twitter_screen_name], twitter_user_id: auth[:twitter_user_id]) | |
end | |
if auth[:facebook].present? && FacebookUserInfo.find_by_facebook_user_id(auth[:facebook][:facebook_user_id]).nil? | |
FacebookUserInfo.create!(auth[:facebook].merge(user_id: user.id)) | |
end | |
if auth[:github_user_id] && auth[:github_screen_name] && GithubUserInfo.find_by_github_user_id(auth[:github_user_id]).nil? | |
GithubUserInfo.create(user_id: user.id, screen_name: auth[:github_screen_name], github_user_id: auth[:github_user_id]) | |
end | |
end | |
# Clear authentication session. | |
session[:authentication] = nil | |
# JSON result | |
render json: {success: true, active: active_result, message: msg } | |
else | |
render json: {success: false, message: I18n.t("login.errors", errors: user.errors.full_messages.join("\n"))} | |
end | |
rescue ActiveRecord::StatementInvalid | |
render json: {success: false, message: I18n.t("login.something_already_taken")} | |
rescue DiscourseHub::NicknameUnavailable | |
render json: {success: false, message: I18n.t("login.errors", errors:I18n.t("login.not_available", suggestion: User.suggest_username(params[:username])) )} | |
rescue RestClient::Forbidden | |
render json: {errors: [I18n.t("discourse_hub.access_token_problem")]} | |
end |
This file contains 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
def create | |
@comment_body = params[:comment_body] | |
respond_with(@opportunity) do |format| | |
if @opportunity.save_with_account_and_permissions(params) | |
@opportunity.add_comment_by_user(@comment_body, current_user) | |
if called_from_index_page? | |
@opportunities = get_opportunities | |
get_data_for_sidebar | |
elsif called_from_landing_page?(:accounts) | |
get_data_for_sidebar(:account) | |
elsif called_from_landing_page?(:campaigns) | |
get_data_for_sidebar(:campaign) | |
end | |
else | |
@accounts = Account.my.order('name') | |
unless params[:account][:id].blank? | |
@account = Account.find(params[:account][:id]) | |
else | |
if request.referer =~ /\/accounts\/(.+)$/ | |
@account = Account.find($1) # related account | |
else | |
@account = Account.new(:user => current_user) | |
end | |
end | |
@contact = Contact.find(params[:contact]) unless params[:contact].blank? | |
@campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank? | |
end | |
end | |
end |
This file contains 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
def initialize_resources_class_accessors! #:nodoc: | |
# First priority is the namespaced model, e.g. User::Group | |
self.resource_class ||= begin | |
namespaced_class = self.name.sub(/Controller/, '').singularize | |
namespaced_class.constantize | |
rescue NameError | |
nil | |
end | |
# Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController | |
self.resource_class ||= begin | |
namespaced_classes = self.name.sub(/Controller/, '').split('::') | |
namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize | |
namespaced_class.constantize | |
rescue NameError | |
nil | |
end | |
# Third priority the camelcased c, i.e. UserGroup | |
self.resource_class ||= begin | |
camelcased_class = self.name.sub(/Controller/, '').gsub('::', '').singularize | |
camelcased_class.constantize | |
rescue NameError | |
nil | |
end | |
# Otherwise use the Group class, or fail | |
self.resource_class ||= begin | |
class_name = self.controller_name.classify | |
class_name.constantize | |
rescue NameError => e | |
raise unless e.message.include?(class_name) | |
nil | |
end | |
self.parents_symbols = self.parents_symbols.try(:dup) || [] | |
# Initialize resources configuration hash | |
self.resources_configuration = self.resources_configuration.try(:dup) || {} | |
self.resources_configuration.each do |key, value| | |
next unless value.is_a?(Hash) || value.is_a?(Array) | |
self.resources_configuration[key] = value.dup | |
end | |
config = (self.resources_configuration[:self] ||= {}) | |
config[:collection_name] = self.controller_name.to_sym | |
config[:instance_name] = self.controller_name.singularize.to_sym | |
config[:route_collection_name] = config[:collection_name] | |
config[:route_instance_name] = config[:instance_name] | |
# Deal with namespaced controllers | |
namespaces = self.controller_path.split('/')[0..-2] | |
config[:route_prefix] = namespaces.join('_') unless namespaces.empty? | |
# Deal with default request parameters in namespaced controllers, e.g. | |
# Forum::Thread#create will properly pick up the request parameter | |
# which will be forum_thread, and not thread | |
# Additionally make this work orthogonally with instance_name | |
config[:request_name] = self.resource_class.to_s.underscore.gsub('/', '_') | |
# Initialize polymorphic, singleton, scopes and belongs_to parameters | |
polymorphic = self.resources_configuration[:polymorphic] || { :symbols => [], :optional => false } | |
polymorphic[:symbols] = polymorphic[:symbols].dup | |
self.resources_configuration[:polymorphic] = polymorphic | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment