Skip to content

Commit

Permalink
add demographic info to profile (backend)
Browse files Browse the repository at this point in the history
  • Loading branch information
algodave committed Feb 24, 2016
1 parent a3691ed commit 1b0a3f0
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 37 deletions.
6 changes: 5 additions & 1 deletion backend/app/controllers/api/v1/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def update
private

def update_params
params.require(:profile).permit(:country_id, :birth_date, :sex_id, :onboarding_step_id)
params.require(:profile).permit(
:country_id, :birth_date, :sex_id, :onboarding_step_id,
:day_habit_id, :education_level_id, :day_walking_hours,
ethnicity_ids: []
)
end
end
47 changes: 39 additions & 8 deletions backend/app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
#
# Table name: profiles
#
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# ethnicity_ids_string :string
# day_habit_id :string
# education_level_id :string
# day_walking_hours :integer
#

class Profile < ActiveRecord::Base
Expand All @@ -32,6 +36,26 @@ class Profile < ActiveRecord::Base
message: '%{value} is not a valid sex_id'
}, if: 'sex_id.present?'

validates :day_habit_id, inclusion: {
in: DayHabit.all_ids,
message: '%{value} is not a valid day_habit_id'
}, if: 'day_habit_id.present?'

validates :education_level_id, inclusion: {
in: EducationLevel.all_ids,
message: '%{value} is not a valid education_level_id'
}, if: 'education_level_id.present?'

validate :ethnicity_ids_are_valid, if: 'ethnicity_ids_string.present?'
def ethnicity_ids_are_valid
ethnicity_ids.each do |id|
unless Ethnicity.all_ids.include?(id)
errors.add(:ethnicity_ids, "#{id} is not a valid ethnicity_id")
break
end
end
end

#
# Instance Methods
#
Expand All @@ -43,4 +67,11 @@ def country
def locale
country.languages.first if country.present?
end

def ethnicity_ids
ethnicity_ids_string.split(',') rescue []
end
def ethnicity_ids=(ids)
self.ethnicity_ids_string = ids.join(',')
end
end
24 changes: 15 additions & 9 deletions backend/app/serializers/profile_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
#
# Table name: profiles
#
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# ethnicity_ids_string :string
# day_habit_id :string
# education_level_id :string
# day_walking_hours :integer
#

class ProfileSerializer < ApplicationSerializer
attributes :id, :birth_date, :country_id, :sex_id, :onboarding_step_id
attributes :id, :birth_date, :country_id, :sex_id, :onboarding_step_id,
:ethnicity_ids, :day_habit_id, :education_level_id, :day_walking_hours

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddDemographicsToProfiles < ActiveRecord::Migration
def change
add_column :profiles, :ethnicity_ids_string, :string
add_column :profiles, :day_habit_id, :string
add_column :profiles, :education_level_id, :string
add_column :profiles, :day_walking_hours, :integer
end
end
10 changes: 7 additions & 3 deletions backend/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160211105611) do
ActiveRecord::Schema.define(version: 20160224141256) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -40,8 +40,12 @@
t.date "birth_date"
t.string "sex_id"
t.string "onboarding_step_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "ethnicity_ids_string"
t.string "day_habit_id"
t.string "education_level_id"
t.integer "day_walking_hours"
end

add_index "profiles", ["user_id"], name: "index_profiles_on_user_id", using: :btree
Expand Down
20 changes: 12 additions & 8 deletions backend/spec/factories/profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
#
# Table name: profiles
#
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# ethnicity_ids_string :string
# day_habit_id :string
# education_level_id :string
# day_walking_hours :integer
#

FactoryGirl.define do
Expand Down
34 changes: 26 additions & 8 deletions backend/spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
#
# Table name: profiles
#
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# ethnicity_ids_string :string
# day_habit_id :string
# education_level_id :string
# day_walking_hours :integer
#

require 'rails_helper'
Expand All @@ -25,4 +29,18 @@
describe 'Respond to' do
it { is_expected.to respond_to(:birth_date) }
end
describe 'ethnicities' do
context 'get' do
before { subject.ethnicity_ids_string='latino,white' }
it 'transforms to array' do
expect(subject.ethnicity_ids).to eq ['latino','white']
end
end
context 'set' do
before { subject.ethnicity_ids=['latino','white'] }
it 'transforms to string' do
expect(subject.ethnicity_ids_string).to eq 'latino,white'
end
end
end
end

0 comments on commit 1b0a3f0

Please sign in to comment.