Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced Lotus::Action::Params#get #89

Merged
merged 2 commits into from
Feb 21, 2015
Merged

Introduced Lotus::Action::Params#get #89

merged 2 commits into from
Feb 21, 2015

Conversation

jodosha
Copy link
Member

@jodosha jodosha commented Feb 20, 2015

What

Introduced Lotus::Action::Params#get as a safe way to access (nested) attributes.

Why?

  1. Developers can safely access nested params without worrying of NoMethodErrors that can be caused by the subscriber notation. Eg params[:address][:street], if :address isn't sent, the action will blow up. The lack of this new suggested API (#get) will force developers to write defensive code to access nested attributes.
  2. This serves as supporting feature for Lotus::Helpers' form helper. In case of param validation failures, a form should be able to be rendered again and keep the current values.

Usage

require 'lotus/controller'

module Deliveries
  class Create
    include Lotus::Action

    params do
      param :customer_name
      param :address do
        param :city
      end
    end

    def call(params)
      params.get('customer_name')   # => "Luca"
      params.get('uknown')          # => nil

      params.get('address.city')    # => "Rome"
      params.get('address.unknown') # => nil

      params.get(nil)               # => nil
    end
  end
end

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.02% when pulling e9e8d87 on params-get into 1bbd4f1 on master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.02% when pulling e9e8d87 on params-get into 1bbd4f1 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.02% when pulling e9e8d87 on params-get into 1bbd4f1 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.02% when pulling 95e5246 on params-get into 1bbd4f1 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.02% when pulling cf2a1cb on params-get into 1bbd4f1 on master.

@vyper
Copy link
Contributor

vyper commented Feb 20, 2015

Why we only use string for get a key?

I ❤️ this approach...

params.get('customer_name')  # => "Luca"
params.get(:customer_name)   # => "Luca"

@runlevel5
Copy link
Member

@vyper hash is nice but it does not represent the nested attributes very well. For that very reason, I think we should stick to string.

@runlevel5
Copy link
Member

@jodosha 👍

@jodosha jodosha self-assigned this Feb 21, 2015
@jodosha jodosha added this to the v0.4.0 milestone Feb 21, 2015
jodosha added a commit that referenced this pull request Feb 21, 2015
Introduced Lotus::Action::Params#get
@jodosha jodosha merged commit 89afe18 into master Feb 21, 2015
@jodosha jodosha deleted the params-get branch February 21, 2015 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants