rubocoped contacts helper

This commit is contained in:
Andres Keskküla 2014-08-26 12:23:31 +03:00
parent d759a232cb
commit 2b1736cd9b
2 changed files with 116 additions and 105 deletions

View file

@ -1,4 +1,5 @@
module Epp::ContactsHelper module Epp
module ContactsHelper
def create_contact def create_contact
@contact = Contact.new(contact_and_address_attributes) @contact = Contact.new(contact_and_address_attributes)
render '/epp/contacts/create' and return if stamp(@contact) && @contact.save render '/epp/contacts/create' and return if stamp(@contact) && @contact.save
@ -9,10 +10,10 @@ module Epp::ContactsHelper
def update_contact def update_contact
code = params_hash['epp']['command']['update']['update'][:id] code = params_hash['epp']['command']['update']['update'][:id]
@contact = Contact.where(code: code).first @contact = Contact.where(code: code).first
if has_rights? && stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update)) if rights? && stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update))
render 'epp/contacts/update' render 'epp/contacts/update'
else else
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: code } } if @contact == [] contact_exists?
handle_errors(@contact) handle_errors(@contact)
end end
end end
@ -34,7 +35,7 @@ module Epp::ContactsHelper
end end
def info_contact def info_contact
handle_errors and return unless has_rights? handle_errors and return unless rights?
@contact = find_contact @contact = find_contact
handle_errors(@contact) and return unless @contact handle_errors(@contact) and return unless @contact
render 'epp/contacts/info' render 'epp/contacts/info'
@ -47,15 +48,15 @@ module Epp::ContactsHelper
## CREATE ## CREATE
def validate_contact_create_request def validate_contact_create_request
@ph = params_hash['epp']['command']['create']['create'] @ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['id'], xml_attrs_present?(@ph, [%w(id),
%w(authInfo pw), %w(authInfo pw),
%w(postalInfo)]) %w(postalInfo)])
if @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array) return epp_errors.empty? unless @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
xml_nested_attrs_present?( @ph['postalInfo'], [ %w(name),
xml_nested_attrs_present?(@ph['postalInfo'], [%w(name),
%w(addr city), %w(addr city),
%w(addr cc)]) %w(addr cc)])
end end
end
## UPDATE ## UPDATE
def validate_contact_update_request def validate_contact_update_request
@ -63,6 +64,12 @@ module Epp::ContactsHelper
xml_attrs_present?(@ph, [['id']]) xml_attrs_present?(@ph, [['id']])
end end
def contact_exists?
return true if @contact.is_a?(Contact)
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'),
value: { obj: 'id', val: code } }
end
## DELETE ## DELETE
def validate_contact_delete_request def validate_contact_delete_request
@ph = params_hash['epp']['command']['delete']['delete'] @ph = params_hash['epp']['command']['delete']['delete']
@ -86,14 +93,15 @@ module Epp::ContactsHelper
def find_contact def find_contact
contact = Contact.find_by(code: @ph[:id]) contact = Contact.find_by(code: @ph[:id])
unless contact unless contact
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: @ph[:id] } } epp_errors << { code: '2303',
msg: t('errors.messages.epp_obj_does_not_exist'),
value: { obj: 'id', val: @ph[:id] } }
end end
contact contact
end end
def has_rights? def rights?
pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || []
id = @ph[:id]
return true if !find_contact.nil? && find_contact.auth_info_matches(pw) return true if !find_contact.nil? && find_contact.auth_info_matches(pw)
@ -104,20 +112,22 @@ module Epp::ContactsHelper
def contact_and_address_attributes(type = :create) def contact_and_address_attributes(type = :create)
case type case type
when :update when :update
contact_hash = Contact.extract_attributes(@ph[:chg], type) contact_hash = merge_attribute_hash(@ph[:chg], type)
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo) || [] ), type)
)
else else
contact_hash = Contact.extract_attributes(@ph, type) contact_hash = merge_attribute_hash(@ph, type)
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
)
end end
contact_hash[:ident_type] = ident_type unless ident_type.nil? contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash contact_hash
end end
def merge_attribute_hash(prms, type)
contact_hash = Contact.extract_attributes(prms, type)
contact_hash = contact_hash.merge(
Address.extract_attributes((prms.try(:[], :postalInfo) || []))
)
contact_hash
end
def ident_type def ident_type
result = params[:frame].slice(/(?<=\<ns2:ident type=)(.*)(?=<)/) result = params[:frame].slice(/(?<=\<ns2:ident type=)(.*)(?=<)/)
@ -126,4 +136,5 @@ module Epp::ContactsHelper
Contact::IDENT_TYPES.any? { |type| return type if result.include?(type) } Contact::IDENT_TYPES.any? { |type| return type if result.include?(type) }
nil nil
end end
end
end end

View file

@ -13,7 +13,7 @@ class Address < ActiveRecord::Base
#validates_inclusion_of :type, in: TYPES #validates_inclusion_of :type, in: TYPES
class << self class << self
def extract_attributes(ah, _type = :create) def extract_attributes(ah)
address_hash = {} address_hash = {}
[ah].flatten.each do |pi| [ah].flatten.each do |pi|
address_hash[local?(pi)] = addr_hash_from_params(pi) address_hash[local?(pi)] = addr_hash_from_params(pi)