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,129 +1,140 @@
module Epp::ContactsHelper module Epp
def create_contact module ContactsHelper
@contact = Contact.new(contact_and_address_attributes) def create_contact
render '/epp/contacts/create' and return if stamp(@contact) && @contact.save @contact = Contact.new(contact_and_address_attributes)
render '/epp/contacts/create' and return if stamp(@contact) && @contact.save
handle_errors(@contact)
end
def update_contact
code = params_hash['epp']['command']['update']['update'][:id]
@contact = Contact.where(code: code).first
if has_rights? && stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update))
render 'epp/contacts/update'
else
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: code } } if @contact == []
handle_errors(@contact) handle_errors(@contact)
end end
end
def delete_contact def update_contact
Contact.transaction do code = params_hash['epp']['command']['update']['update'][:id]
@contact = Contact.where(code: code).first
if rights? && stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update))
render 'epp/contacts/update'
else
contact_exists?
handle_errors(@contact)
end
end
def delete_contact
Contact.transaction do
@contact = find_contact
handle_errors(@contact) and return unless @contact
handle_errors(@contact) and return unless @contact.destroy_and_clean
render '/epp/contacts/delete'
end
end
def check_contact
ph = params_hash['epp']['command']['check']['check']
@contacts = Contact.check_availability(ph[:id])
render '/epp/contacts/check'
end
def info_contact
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
handle_errors(@contact) and return unless @contact.destroy_and_clean render 'epp/contacts/info'
render '/epp/contacts/delete'
end end
end
def check_contact ## HELPER METHODS
ph = params_hash['epp']['command']['check']['check']
@contacts = Contact.check_availability(ph[:id])
render '/epp/contacts/check'
end
def info_contact private
handle_errors and return unless has_rights?
@contact = find_contact
handle_errors(@contact) and return unless @contact
render 'epp/contacts/info'
end
## HELPER METHODS ## CREATE
def validate_contact_create_request
@ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [%w(id),
%w(authInfo pw),
%w(postalInfo)])
return epp_errors.empty? unless @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
private xml_nested_attrs_present?(@ph['postalInfo'], [%w(name),
%w(addr city),
## CREATE %w(addr cc)])
def validate_contact_create_request
@ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['id'],
%w(authInfo pw),
%w(postalInfo)])
if @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
xml_nested_attrs_present?( @ph['postalInfo'], [ %w(name),
%w(addr city),
%w(addr cc)])
end end
end
## UPDATE ## UPDATE
def validate_contact_update_request def validate_contact_update_request
@ph = params_hash['epp']['command']['update']['update'] @ph = params_hash['epp']['command']['update']['update']
xml_attrs_present?(@ph, [['id']]) xml_attrs_present?(@ph, [['id']])
end
## DELETE
def validate_contact_delete_request
@ph = params_hash['epp']['command']['delete']['delete']
xml_attrs_present?(@ph, [['id']])
end
## CHECK
def validate_contact_check_request
@ph = params_hash['epp']['command']['check']['check']
xml_attrs_present?(@ph, [['id']])
end
## INFO
def validate_contact_info_request
@ph = params_hash['epp']['command']['info']['info']
xml_attrs_present?(@ph, [['id']])
end
## SHARED
def find_contact
contact = Contact.find_by(code: @ph[:id])
unless contact
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: @ph[:id] } }
end end
contact
end
def has_rights? def contact_exists?
pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] return true if @contact.is_a?(Contact)
id = @ph[:id] epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'),
value: { obj: 'id', val: code } }
end
return true if !find_contact.nil? && find_contact.auth_info_matches(pw) ## DELETE
def validate_contact_delete_request
@ph = params_hash['epp']['command']['delete']['delete']
xml_attrs_present?(@ph, [['id']])
end
epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: pw } } ## CHECK
false def validate_contact_check_request
end @ph = params_hash['epp']['command']['check']['check']
xml_attrs_present?(@ph, [['id']])
end
def contact_and_address_attributes(type = :create) ## INFO
case type def validate_contact_info_request
when :update @ph = params_hash['epp']['command']['info']['info']
contact_hash = Contact.extract_attributes(@ph[:chg], type) xml_attrs_present?(@ph, [['id']])
end
## SHARED
def find_contact
contact = Contact.find_by(code: @ph[:id])
unless contact
epp_errors << { code: '2303',
msg: t('errors.messages.epp_obj_does_not_exist'),
value: { obj: 'id', val: @ph[:id] } }
end
contact
end
def rights?
pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || []
return true if !find_contact.nil? && find_contact.auth_info_matches(pw)
epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: pw } }
false
end
def contact_and_address_attributes(type = :create)
case type
when :update
contact_hash = merge_attribute_hash(@ph[:chg], type)
else
contact_hash = merge_attribute_hash(@ph, type)
end
contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash
end
def merge_attribute_hash(prms, type)
contact_hash = Contact.extract_attributes(prms, type)
contact_hash = contact_hash.merge( contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo) || [] ), type) Address.extract_attributes((prms.try(:[], :postalInfo) || []))
)
else
contact_hash = Contact.extract_attributes(@ph, type)
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
) )
contact_hash
end end
contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash
end
def ident_type def ident_type
result = params[:frame].slice(/(?<=\<ns2:ident type=)(.*)(?=<)/) result = params[:frame].slice(/(?<=\<ns2:ident type=)(.*)(?=<)/)
return nil unless result return nil unless result
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)
@ -27,7 +27,7 @@ class Address < ActiveRecord::Base
def local?(postal_info) def local?(postal_info)
return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
:international_address_attributes :international_address_attributes
end end
def addr_hash_from_params(addr) def addr_hash_from_params(addr)
return {} unless addr[:addr].is_a?(Hash) return {} unless addr[:addr].is_a?(Hash)