Instantly share code, notes, and snippets.

Embed
What would you like to do?
Refactored create
# POST /issues
# POST /issues.xml
def create
@issue = Issue.new(params[:issue])
# A D D N E W C A U S E / E F F E C T
if params[:action_carrier]
# Read in the :type passed with form to recognize whether this is a Cause or Effect
@causality = params[:action_carrier].to_s
@causality_id = params[:id_carrier]
if Issue.exists?(:wiki_url => [@issue.wiki_url])
add_already_existent_issue
else
add_new_issue
Suggestion.new(params[:issue_id=>@issue.id, :wiki_url=>@issue.wiki_url]) # Suggestions for new issue
end
else
respond_to do |format|
if @issue.save
initialize_suggestion_object
format.html { redirect_to(@issue, :notice => 'Issue was successfully created.') }
format.xml { render :xml => @issue, :status => :created, :location => @issue }
else
format.html { render :action => "new" }
format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
end
end
end
end
def add_already_existent_issue
retrieve_id_of_issue
@relationship = Relationship.new
set_relationship_user_id_if_applicable
set_type_of_relationship(true)
save_relationship
end
def retrieve_id_of_issue
@wikiurl = @issue.wiki_url
@issueid = Issue.where(:wiki_url => @wikiurl).select('id').first.id
end
def set_relationship_user_id_if_applicable
if @issue.user_id.to_s != ""
@relationship.user_id = @issue.user_id
end
end
def add_new_issue
if @issue.save
initialize_suggestion_object
Reputation::Utils.reputation(:action=>:create, \
:type=>:issue, \
:me=>@issue.user_id, \
:undo=>false, \
:calculate=>true)
@relationship = Relationship.new
set_relationship_user_id_if_applicable
set_type_of_relationship(false)
save_relationship
else
@notice = @issue.errors.full_messages
redirect_to(:back, :notice => @notice.to_s + ' Causal link was not created - Issue did not exist')
end
end
def initialize_suggestion_object
suggested_causes, suggested_effects, suggested_inhibitors, suggested_reduced, suggested_parents, suggested_subsets = Suggestion.new.get_suggestions(@issue.wiki_url, @issue.id)
Suggestion.create(suggested_causes)
Suggestion.create(suggested_effects)
Suggestion.create(suggested_inhibitors)
Suggestion.create(suggested_reduced)
Suggestion.create(suggested_parents)
Suggestion.create(suggested_subsets)
end
def set_type_of_relationship(already_exists)
args = {
C: [nil, 'a cause', 'cause'],
I: [:I, 'a reducer', 'reducer issue'],
P: [:H, 'a superset', 'superset'],
E: [nil, 'an effect', 'effect'],
R: [:I, 'reduced', 'reduced issue'],
S: [:H, 'a subset', 'subset']
}[@causality.to_sym]
(@notice = 'Error creating and linking issue' and return) if args.nil?
if already_exists
case @causality
when "C", "I", "P"
@relationship.cause_id = @issueid
@relationship.issue_id = @causality_id
when "E", "R", "S"
@relationship.cause_id = @causality_id
@relationship.issue_id = @issueid
end
@relationship.relationship_type = args[0].try(:to_s)
else
case @causality
when "C", "I", "P"
@relationship.cause_id = @issue.id
@relationship.issue_id = @causality_id
when "E", "R", "S"
@relationship.cause_id = @causality_id
@relationship.issue_id = @issue.id
end
@relationship.relationship_type = args[0].try(:to_s)
end
@notice = if already_exists
"New #{args[2]} linked Successfully"
else
"New Issue was created and linked as #{args[1]}"
end
end
def save_relationship
if @relationship.save
remove_duplicate_suggestions
Reputation::Utils.reputation(:action=>:create, \
:type=>:relationship, \
:id=>@relationship.id, \
:me=>@relationship.user_id, \
:undo=>false, \
:calculate=>true)
redirect_to(:back, :notice => @notice)
else
error_saving_causal_link
end
end
def remove_duplicate_suggestions
if Suggestion.exists?(:causality => @causality, :wiki_url => [@issue.wiki_url], :issue_id=>@causality_id)
@suggestion_id = Suggestion.where(:causality => @causality, :wiki_url => [@issue.wiki_url], :issue_id=>@causality_id).select('id').first.id
@suggestion = Suggestion.find(@suggestion_id)
@suggestion.update_attributes('status' => 'A')
@suggestion.save
end
end
def error_saving_causal_link
@notice = @relationship.errors.full_messages
redirect_to(:back, :notice => @notice.to_s + ' Causal link was not created')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment