5
\$\begingroup\$

I have to call Article.to_draft/to_archive/publish method depending on the presence of the corresponding key in the params hash.

I do, however, have no idea to implement it properly.

def update_state
 method = if params[:draft].present?
 :to_draft
 elsif params[:archived].present?
 :to_archive
 else
 :publish
 end
 @article.send method
end

The code works, but the number of possible states of an article will probably grow in the future.

I would like to have a hash like this:

{ draft: :to_draft, archived: :to_archive, default: :publish }

and let #update_state decide what should be called, based on the params hash.

asked Jan 7, 2016 at 14:45
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I suggest using find

ARTICLE_ACTIONS = { :draft => :to_draft,
 :archived => :to_archive,
 :default => :publish 
 }
def update_state
 state = [:draft, :archived, :default]
 .find {|state| params[state].present?}
 @article.send ARTICLE_ACTIONS[state]
end
answered Jan 7, 2016 at 14:54
\$\endgroup\$
3
  • \$\begingroup\$ Sorry, I probably didn't explain it well. params is a hash provided by rails controller. it does NOT contain the names of the corresponding methods (:draft => :to_draft etc.). I have to call what's needed manually \$\endgroup\$ Commented Jan 7, 2016 at 14:59
  • \$\begingroup\$ Browser sends an HTTP request, with one of the possible states (draft, archived). Based on this, I have to call one of the @article methods: @article.to_draft if params contains 'draft' or @articleto_archive if params contains archived or @article.publish if none are provided \$\endgroup\$ Commented Jan 7, 2016 at 15:03
  • \$\begingroup\$ @justso you may build the hash yourself, answer updated \$\endgroup\$ Commented Jan 7, 2016 at 15:03
0
\$\begingroup\$

I know the question is already but I think it can be simplified further by using routes. Like

post "article/:state", to: "articles#update_state"

and then you can simply call

["draft", "archived", "default"].includes?(params[:state]) && @article.send(params[:state]) 
answered Jan 13, 2016 at 11:05
\$\endgroup\$
3
  • \$\begingroup\$ It's an security vulnerability \$\endgroup\$ Commented Jan 13, 2016 at 11:07
  • 2
    \$\begingroup\$ A question having an accepted answer isn't a problem, more reviews are always welcome when they have new suggestions like this. \$\endgroup\$ Commented Jan 13, 2016 at 11:09
  • \$\begingroup\$ @justso I think it can be bypassed, I have edited my answer though. \$\endgroup\$ Commented Jan 13, 2016 at 11:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.