Add contry select to user form

This commit is contained in:
Martin Lensment 2014-09-30 12:43:02 +03:00
parent f76d73c597
commit 0dc0cc63d9
6 changed files with 16 additions and 16 deletions

View file

@ -43,6 +43,6 @@ class Admin::UsersController < AdminController
end
def user_params
params.require(:user).permit(:username, :password, :identity_code, :email, :registrar_id, :admin, :registrar_typeahead)
params.require(:user).permit(:username, :password, :identity_code, :email, :registrar_id, :admin, :registrar_typeahead, :country_id)
end
end

View file

@ -8,12 +8,14 @@ class User < ActiveRecord::Base
belongs_to :role
belongs_to :registrar
belongs_to :country
validates :username, :password, presence: true
validates :identity_code, uniqueness: true, allow_blank: true
validate :registrar_presence
validates :identity_code, presence: true, if: -> { country.iso == 'EE' && identity_code.blank? }
validates :registrar, presence: true, if: -> { !admin && !registrar }
before_save :manage_registrar
before_save -> { self.registrar = nil if admin? }
attr_accessor :registrar_typeahead
@ -24,16 +26,4 @@ class User < ActiveRecord::Base
def registrar_typeahead
@registrar_typeahead || registrar || nil
end
private
def registrar_presence
if !admin && !registrar
errors.add(:registrar, :blank)
end
end
def manage_registrar
self.registrar = nil if admin?
end
end

View file

@ -14,6 +14,9 @@
.form-group
= f.label :password
= f.text_field(:password, class: 'form-control')
.form-group
= f.label :country_id
= f.collection_select :country_id, Country.all, :id, :name, {}, { class: 'form-control' }
.form-group
= f.label :identity_code
= f.text_field(:identity_code, class: 'form-control')

View file

@ -164,6 +164,7 @@ en:
password:
blank: 'Password is missing'
identity_code:
blank: 'Identity code is missing'
taken: 'Identity code already exists'
email:
blank: 'Email is missing'

View file

@ -0,0 +1,5 @@
class AddCountryToUsers < ActiveRecord::Migration
def change
add_column :users, :country_id, :integer
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140929095329) do
ActiveRecord::Schema.define(version: 20140930093039) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -200,6 +200,7 @@ ActiveRecord::Schema.define(version: 20140929095329) do
t.boolean "admin", default: false
t.string "identity_code"
t.integer "registrar_id"
t.integer "country_id"
end
end