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

Manage and display members (#8) #74

Merged
merged 24 commits into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions app/controllers/admin/members_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Admin
class MembersController < Admin::ApplicationController
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Member.all.paginate(10, params[:page])
# end

# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Member.find_by!(slug: param)
# end

# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
end
end
19 changes: 19 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Admin
class UsersController < Admin::ApplicationController
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Member.all.paginate(10, params[:page])
# end

# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Member.find_by!(slug: param)
# end

# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
end
end
4 changes: 3 additions & 1 deletion app/dashboards/dashboard_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class DashboardManifest
:locations,
:jobs,
:organizations,
:pages
:pages,
:members,
:users,
].freeze

# `ROOT_DASHBOARD`
Expand Down
58 changes: 58 additions & 0 deletions app/dashboards/member_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "administrate/base_dashboard"

class MemberDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

events: Field::HasMany,
organizations: Field::HasMany,
Copy link
Member

Choose a reason for hiding this comment

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

After playing around to understand administrate and that bug: https://github.com/montrealrb/Montreal.rb/pull/74/files#r47185636

I found out that removing all references in the dashboard to events and organizations for now fixes it.

user: Field::BelongsTo,
id: Field::Number,
name: Field::String,
email: Field::String,
picture: Field::String.with_options(searchable: false),
twitter_handle: Field::String,
github_handle: Field::String,
biography: Field::Text,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

:user,
Copy link
Member

Choose a reason for hiding this comment

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

After removing :events and :organization, some admin pages could not be rendered because the user dashboard is not in the manifest and it tries to create a link to the user dashboard. For now, we should replace :user for :name.

Also, to be able to display #show properly in the dashboard, the user dashboard has to be present in the manifest. However, there are some bugs in it because of the :votes field that should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're a bit confused. We should replace :user with :name in the manifest, correct? We're not familiar with the code written for the User model, should we just remove :votes from the manifest but keep the column in the db?

Copy link
Contributor

Choose a reason for hiding this comment

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

@coaxial, please:

  • Rebase with master and fix the conflicts.
  • Don't replace :user with :name that was a hack to get it to work, do the right thing ;)
  • Get familar with the user model :)
  • It's better if you remove the user_dashboard and generate a new one based on the last changes like so: rails g administrate:dashboard user
  • Let me know if you need a hand to finish this

:id,
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

:user,
:name,
:email,
:picture,
:twitter_handle,
:github_handle,
:biography,
].freeze

# Overwrite this method to customize how members are displayed
# across all pages of the admin dashboard.
#
# def display_resource(member)
# "Member ##{member.id}"
# end
end
28 changes: 2 additions & 26 deletions app/dashboards/user_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ class UserDashboard < Administrate::BaseDashboard
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

votes: Field::HasMany,
id: Field::Number,
email: Field::String,
encrypted_password: Field::String,
reset_password_token: Field::String,
reset_password_sent_at: Field::DateTime,
remember_created_at: Field::DateTime,
sign_in_count: Field::Number,
current_sign_in_at: Field::DateTime,
last_sign_in_at: Field::DateTime,
current_sign_in_ip: Field::String.with_options(searchable: false),
last_sign_in_ip: Field::String.with_options(searchable: false),
created_at: Field::DateTime,
updated_at: Field::DateTime,
bio: Field::String
member: Field::HasOne,
}.freeze

# COLLECTION_ATTRIBUTES
Expand All @@ -31,10 +19,9 @@ class UserDashboard < Administrate::BaseDashboard
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

:votes,
:id,
:email,
:encrypted_password
:member,
].freeze

# SHOW_PAGE_ATTRIBUTES
Expand All @@ -45,18 +32,7 @@ class UserDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

:votes,
:email,
:encrypted_password,
:reset_password_token,
:reset_password_sent_at,
:remember_created_at,
:sign_in_count,
:current_sign_in_at,
:last_sign_in_at,
:current_sign_in_ip,
:last_sign_in_ip,
:bio
].freeze

# Overwrite this method to customize how users are displayed
Expand Down
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Event < ActiveRecord::Base
belongs_to :location
belongs_to :author, foreign_key: :user_id, class_name: "User"
has_many :talks, -> { where(state: "scheduled") }, class_name: "Talk"
has_and_belongs_to_many :members

to_param :title

Expand Down
8 changes: 8 additions & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Member < ActiveRecord::Base
has_and_belongs_to_many :events
has_and_belongs_to_many :organizations
belongs_to :user

validates :name, :email, presence: true
validates :email, uniqueness: true
end
2 changes: 2 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Organization < ActiveRecord::Base
has_many :jobs
has_and_belongs_to_many :jobs

translates :description
mount_uploader :logo, LogoUploader

Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: [:github]

has_one :member

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
Expand Down Expand Up @@ -53,4 +55,5 @@ def self.default_user
def active_for_authentication?
super && email != DEFAULT_USER_EMAIL
end

Choose a reason for hiding this comment

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

Extra empty line detected at class body end.

end
14 changes: 14 additions & 0 deletions db/migrate/20151125011253_create_members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateMembers < ActiveRecord::Migration
def change
create_table :members do |t|
t.string :name
t.string :email
t.binary :picture
t.string :twitter_handle
t.string :github_handle
t.text :biography

t.timestamps null: false
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203012632_add_null_constraint_to_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNullConstraintToEmail < ActiveRecord::Migration
def change
change_column_null :members, :email, false
end
end
6 changes: 6 additions & 0 deletions db/migrate/20151203031902_add_foreign_key_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddForeignKeyToUsers < ActiveRecord::Migration
def change
add_column :users, :member_id, :integer
add_foreign_key :users, :members
end
end
6 changes: 6 additions & 0 deletions db/migrate/20151206020220_remove_foreign_key_from_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveForeignKeyFromUsers < ActiveRecord::Migration
def change
remove_foreign_key :users, :members
remove_column :users, :member_id
end
end
6 changes: 6 additions & 0 deletions db/migrate/20151206044852_add_foreign_key_to_members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddForeignKeyToMembers < ActiveRecord::Migration
def change
add_column :members, :user_id, :integer
add_foreign_key :members, :users
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateHabtmRelationshipBetweenMembersAndEvents < ActiveRecord::Migration
def change
create_table :events_members do |t|
t.belongs_to :event, index: true
t.belongs_to :member, index: true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class HabtmRelationshipBetweenMembersAndOrganization < ActiveRecord::Migration
def change
create_table :members_organizations do |t|
t.belongs_to :organization, index: true
t.belongs_to :member, index: true
end
end
end
33 changes: 32 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: 20160308004823) do
ActiveRecord::Schema.define(version: 20160310032845) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -41,6 +41,14 @@
add_index "events", ["starts_at"], name: "index_events_on_starts_at", using: :btree
add_index "events", ["user_id"], name: "index_events_on_user_id", using: :btree

create_table "events_members", force: :cascade do |t|
t.integer "event_id"
t.integer "member_id"
end

add_index "events_members", ["event_id"], name: "index_events_members_on_event_id", using: :btree
add_index "events_members", ["member_id"], name: "index_events_members_on_member_id", using: :btree

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
Expand Down Expand Up @@ -86,6 +94,26 @@
t.datetime "updated_at"
end

create_table "members", force: :cascade do |t|
t.string "name"
t.string "email", null: false
t.binary "picture"
t.string "twitter_handle"
t.string "github_handle"
t.text "biography"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end

create_table "members_organizations", force: :cascade do |t|
t.integer "organization_id"
t.integer "member_id"
end

add_index "members_organizations", ["member_id"], name: "index_members_organizations_on_member_id", using: :btree
add_index "members_organizations", ["organization_id"], name: "index_members_organizations_on_organization_id", using: :btree

create_table "news_items", force: :cascade do |t|
t.string "state"
t.datetime "published_at"
Expand Down Expand Up @@ -138,6 +166,8 @@
t.string "slug", default: "temporary-slug", null: false
end

add_index "pages", ["slug"], name: "index_pages_on_slug", unique: true, using: :btree

create_table "talk_translations", force: :cascade do |t|
t.integer "talk_id", null: false
t.string "locale", null: false
Expand Down Expand Up @@ -184,5 +214,6 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["uid"], name: "index_users_on_uid", using: :btree

add_foreign_key "members", "users"
add_foreign_key "talks", "events"
end
Empty file.
4 changes: 2 additions & 2 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "rails_helper"

RSpec.describe EventsController, type: :controller do
let(:proposed_event) { create(:event, :proposed) }
let(:scheduled_event) { create(:event, :scheduled) }
let!(:proposed_event) { create(:event, :proposed) }
let!(:scheduled_event) { create(:event, :scheduled) }

describe "GET #index" do
before :each do
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FactoryGirl.define do
factory :member do
name "MyString"
email "MyString"
picture ""
twitter_handle "MyString"
github_handle "MyString"
biography "MyText"
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Ca we correct indentation here please?

39 changes: 39 additions & 0 deletions spec/models/member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "rails_helper"

RSpec.describe Member, type: :model do
context "without a name" do
it "should be invalid" do
member = Member.new

member.email = "gala@example.com"

expect(member).to be_invalid
end
end

context "without an email" do
it "should be invalid" do
member = Member.new

member.name = "Zaba"

expect(member).to be_invalid
end
end

context "with a duplicate email" do
it "should be invalid" do
member1 = Member.new
member1.name = "Zaba"
member1.email = "gala@example.com"

member2 = Member.new
member2.name = "Gala"
member2.email = "gala@example.com"

member1.save

expect(member2).to be_invalid
end
end
end