stripping "undefined" in after_find and after_save callbacks instead of overriding getters

This commit is contained in:
Maikel Linke
2015-02-19 16:33:43 +11:00
parent a7b723af93
commit d5c2abdd7e
3 changed files with 14 additions and 31 deletions

View File

@@ -8,6 +8,8 @@ class EnterpriseGroup < ActiveRecord::Base
validates :address, presence: true, associated: true
before_validation :set_undefined_address_fields
before_validation :set_unused_address_fields
after_find :unset_undefined_address_fields
after_save :unset_undefined_address_fields
validates :name, presence: true
validates :description, presence: true
@@ -18,7 +20,7 @@ class EnterpriseGroup < ActiveRecord::Base
attr_accessible :address_attributes
attr_accessible :email, :website, :facebook, :instagram, :linkedin, :twitter
delegate :phone, :to => :address
delegate :phone, :address1, :address2, :city, :zipcode, :state, :country, :to => :address
has_attached_file :logo,
styles: {medium: "100x100"},
@@ -59,28 +61,12 @@ class EnterpriseGroup < ActiveRecord::Base
address.zipcode.present? || address.zipcode = 'undefined'
end
def phone
address.andand.phone.andand.sub('undefined', '')
end
def address1
address.andand.address1.andand.sub('undefined', '')
end
def address2
address.andand.address2.andand.sub('undefined', '')
end
def city
address.andand.city.andand.sub('undefined', '')
end
def state
address.andand.state
end
def zipcode
address.andand.zipcode.andand.sub('undefined', '')
def unset_undefined_address_fields
return unless address.present?
address.phone.sub!(/^undefined$/, '')
address.address1.sub!(/^undefined$/, '')
address.city.sub!(/^undefined$/, '')
address.zipcode.sub!(/^undefined$/, '')
end
end

View File

@@ -63,13 +63,11 @@
.row
.alpha.three.columns
= af.label :phone
%span.required *
.omega.eight.columns
= af.text_field :phone, { placeholder: "eg. 98 7654 3210"}
.row
.three.columns.alpha
= af.label :address1
%span.required *
.eight.columns.omega
= af.text_field :address1, { placeholder: "eg. 123 High Street"}
.row
@@ -82,7 +80,6 @@
= af.label :city, 'Suburb'
\/
= af.label :zipcode, 'Postcode'
%span.required *
.four.columns
= af.text_field :city, { placeholder: "eg. Northcote"}
.four.columns.omega
@@ -92,7 +89,6 @@
= af.label :state_id, 'State'
\/
= af.label :country_id, 'Country'
%span.required *
.four.columns
= af.collection_select :state_id, af.object.country.states, :id, :name, {}, :class => "select2 fullwidth"
.four.columns.omega

View File

@@ -42,9 +42,10 @@
- if @group.address2.present?
%br
= @group.address2
%br
= @group.city
= @group.state
= @group.zipcode
- if @group.city.present?
%br
= @group.city
= @group.state
= @group.zipcode