Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

92 automatic profile creation for new user #111

Merged
merged 13 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
ruby "2.3.0"

gem "administrate", "~> 0.2.2"
gem "administrate-field-nested_has_many", "~> 0.0.2"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this gem?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gem "autoprefixer-rails"
gem "delayed_job_active_record"
gem "flutie"
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ GEM
rails (~> 4.2)
sass-rails (~> 5.0)
selectize-rails (~> 0.6)
administrate-field-nested_has_many (0.0.2)
administrate (~> 0.2.0)
cocoon (~> 1.2)
rails (~> 4.2)
arel (6.0.3)
autoprefixer-rails (6.4.0.2)
execjs
Expand Down Expand Up @@ -83,6 +87,7 @@ GEM
capybara (>= 2.3.0, < 2.8.0)
json
choice (0.2.0)
cocoon (1.2.9)
coderay (1.1.1)
concurrent-ruby (1.0.2)
crack (0.4.3)
Expand Down Expand Up @@ -312,6 +317,7 @@ PLATFORMS

DEPENDENCIES
administrate (~> 0.2.2)
administrate-field-nested_has_many (~> 0.0.2)
autoprefixer-rails
awesome_print
bourbon (~> 5.0.0.beta.6)
Expand Down Expand Up @@ -363,4 +369,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.12.5
1.13.3
5 changes: 3 additions & 2 deletions app/dashboards/user_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserDashboard < Administrate::BaseDashboard
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
entries: Field::HasMany,
profile: Field::HasOne,
cohort_id: CohortField,
permissions: Field::HasMany,
job_applications: Field::HasMany,
id: Field::Number,
Expand All @@ -34,7 +34,7 @@ class UserDashboard < Administrate::BaseDashboard
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = [
:profile,
:cohort_id,
:name,
:github_username,
:email,
Expand All @@ -47,6 +47,7 @@ class UserDashboard < Administrate::BaseDashboard
:name,
:github_username,
:email,
:cohort_id
].freeze

# Overwrite this method to customize how users are displayed
Expand Down
7 changes: 7 additions & 0 deletions app/fields/cohort_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "administrate/field/base"

class CohortField < Administrate::Field::Base
def to_s
Cohort.find(data).name
end
end
9 changes: 9 additions & 0 deletions app/models/cohort.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class Cohort < ActiveRecord::Base
has_many :profiles
has_many :users, through: :profiles

def self.next
(upcoming.length > 0) ? upcoming[0].id : nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning the ID is perhaps what you needed for this particular task, but for the purposes of building the API for Cohort, it'd be more useful (and no less computational effort for Ruby) to return the entire Cohort object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

def self.upcoming
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out ActiveRecord scopes. This functionality would be better as a scope inside this model than it is as a class method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cohort.where("start_date > ?", Date.today).order(start_date: :asc)
end

end
16 changes: 14 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class User < ActiveRecord::Base
has_many :entries
has_one :profile
has_one :cohort, through: :profile
has_many :permissions
has_many :events

accepts_nested_attributes_for :profile
has_many :hidings

# accepts_nested_attributes_for :permissions

has_many :job_applications, through: :entries
Expand All @@ -19,6 +19,18 @@ class User < ActiveRecord::Base
delegate :twitter, to: :profile
# delegate :name, :to => :profile

def cohort_id
@cohort_id ||= self.profile.try(:cohort_id)
end

# For every param key passed to User.new, Rails runs a
# setter method named after that key. Most of these are
# default AR setter methods but it will also run this:

def cohort_id=(input)
self.build_profile(cohort_id: input)
end

# Public: Checks if a User's candidate Profile is blank
#
# Returns True if one of the required Profile fields is blank
Expand Down
6 changes: 6 additions & 0 deletions app/views/fields/cohort_field/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.collection_select(field.attribute, Cohort.all, :id, :name, prompt: true, selected: Cohort.next) %>
</div>
1 change: 1 addition & 0 deletions app/views/fields/cohort_field/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
1 change: 1 addition & 0 deletions app/views/fields/cohort_field/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
18 changes: 17 additions & 1 deletion 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: 20160914203736) do
ActiveRecord::Schema.define(version: 20161027233611) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -141,6 +141,13 @@

add_index "profiles", ["user_id"], name: "index_profiles_on_user_id", unique: true, using: :btree

create_table "projects", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", null: false
t.integer "cohort_id"
end

create_table "salaries", force: :cascade do |t|
t.integer "amount"
t.integer "rate"
Expand All @@ -158,6 +165,15 @@
t.datetime "updated_at", null: false
end

create_table "translations", force: :cascade do |t|
t.integer "user_id"
t.string "input_text", null: false
t.string "output_text", limit: 106
t.datetime "created_at"
t.datetime "updated_at"
t.integer "project_id", null: false
end

create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "github_username", null: false
Expand Down