I am creating a Stripe payment engine for the Catarse project. I have three records I need to copy from my User, who is a project owner to my User's project. But because I'm a beginner, the code looks like S&%#te!
#project_controller.rb
def create
...
check_for_stripe_keys
....
end
def show
...
check_for_stripe_keys
....
end
....
def check_for_stripe_keys
if @project.stripe_userid.nil?
@project.reload
@project.stripe_access_token = @project.user.stripe_access_token.dup
@project.stripe_key = @project.user.stripe_key.dup
@project.stripe_userid = @project.user.stripe_userid.dup
elsif @project.stripe_userid != @project.user.userid
@project.stripe_access_token = @project.user.stripe_access_token.dup
@project.stripe_key = @project.user.stripe_key.dup
@project.stripe_userid = @project.user.stripe_userid.dup
end
@project.save
end
....
I only need those three records because my stripe code is an engine. Three things:
1) Initially I thought to user update_attributes but I don't know if its possible to use .dup in that method.
2) Is it possible to put this in a helper located in the engine thats accessible to the main_app so users don't have to edit the main_app project_controller code?
3) Is there a cleaner way to show the above .dup code? 'Cause it's fugly!
Thanks for your help!