diff --git a/Gemfile b/Gemfile
index 09643ba..62a6501 100644
--- a/Gemfile
+++ b/Gemfile
@@ -31,6 +31,8 @@ gem 'sdoc', '~> 0.4.0', group: :doc
#auth
gem 'devise'
gem 'cancancan', '~> 1.7'
+#gem 'will_paginate','~>3.0.5'
+
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index d83690e..e848a2d 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -2,4 +2,14 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
+ #before_action :authenticate_user!
+ before_filter :configure_permitted_parameters, if: :devise_controller?
+
+ protected
+
+ def configure_permitted_parameters
+ devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :password, :kind, :name)}
+ devise_parameter_sanitizer.for(:account_update){|u| u.permit(:email, :password, :current_password, :name)}
+
+ end
end
diff --git a/app/controllers/communities_controller.rb b/app/controllers/communities_controller.rb
new file mode 100644
index 0000000..d3c58bb
--- /dev/null
+++ b/app/controllers/communities_controller.rb
@@ -0,0 +1,53 @@
+class CommunitiesController < ApplicationController
+before_action :find_current_community, only: [:show, :edit, :update]
+before_filter :authenticate_user!
+
+ def index
+ @communities = Community.all
+ end
+
+ def show
+
+ end
+
+ def edit
+ render :new unless @community
+ end
+
+
+ def update
+ if @community.update(community_params)
+ render :show
+ else
+ render :edit
+ end
+ end
+
+
+ def new
+ @community = Community.new
+ end
+
+ def create
+ @community = Community.new(community_params)
+ @community.user_id = current_user.id
+ if @community.save
+ render :show
+ else
+ render :new
+ end
+ end
+
+
+ private
+
+
+ def community_params
+ params.require(:community).permit(:subject)
+ end
+
+
+ def find_current_community
+ @community = Community.where(id: params[:id]).take
+ end
+end
diff --git a/app/controllers/community_memberships_controller.rb b/app/controllers/community_memberships_controller.rb
new file mode 100644
index 0000000..6cb1eda
--- /dev/null
+++ b/app/controllers/community_memberships_controller.rb
@@ -0,0 +1,45 @@
+class CommunityMembershipsController < ApplicationController
+before_action :find_current_community_membership, only: [:show, :destroy]
+before_filter :authenticate_user!
+
+ def index
+
+ end
+
+ def show
+
+ end
+
+
+ def new
+ @community_membership = CommunityMembership.new
+ end
+
+ def create
+ @community_membership = CommunityMembership.new(community_params)
+ @community_membership.community_id = current_user.id
+ if @community_membership.save
+ render :show
+ else
+ render :new
+ end
+ end
+
+ def destroy
+ community_id = @community_membership.community_id
+ @community_membership.destroy
+ redirect_to url_for(:controller => :communities, :action => :show, :id => community_id)
+ end
+
+ private
+
+
+ def community_membership_params
+ params.require(:community_membership).permit(:user_id)
+ end
+
+
+ def find_current_community_membership
+ @community_membership = CommunityMembership.where(id: params[:id]).take
+ end
+end
diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb
new file mode 100644
index 0000000..7aa5057
--- /dev/null
+++ b/app/controllers/people_controller.rb
@@ -0,0 +1,52 @@
+class PeopleController < ApplicationController
+before_action :find_current_person, only: [:show, :edit, :update]
+before_filter :authenticate_user!
+
+ def index
+ @people = Person.all
+ end
+
+ def show
+
+ end
+
+ def edit
+ render :new unless @person
+ end
+
+ def update
+ if @person.update(person_params)
+ render :show
+ else
+ render :edit
+ end
+ end
+
+ def new
+ @person = Person.new
+ end
+
+ def create
+ @person = Person.new(person_params)
+ @person.user_id = current_user.id
+ if @person.save
+ render :show
+ else
+ render :new
+ end
+ end
+
+
+ private
+
+
+ def person_params
+ params.require(:person).permit(:surename, :date_of_birth)
+ end
+
+ def find_current_person
+ @person = Person.where(id: params[:id]).take
+ end
+
+
+end
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
new file mode 100644
index 0000000..0380e35
--- /dev/null
+++ b/app/controllers/posts_controller.rb
@@ -0,0 +1,55 @@
+class PostsController < ApplicationController
+before_action :find_current_post, only: [:show, :edit, :update, :destroy]
+before_filter :authenticate_user!
+
+ def index
+
+ end
+
+ def show
+
+ end
+
+ def edit
+ render :new unless @post
+ end
+
+ def update
+ if @post.update(post_params)
+ render :show
+ else
+ render :edit
+ end
+ end
+
+ def new
+ @post = Post.new
+ end
+
+ def create
+ @post = Post.new(post_params)
+ @post.user_id = current_user.id
+ if @post.save
+ render :show
+ else
+ render :new
+ end
+ end
+
+ def destroy
+ @post.destroy
+ redirect_to user_root_path
+ end
+
+ private
+
+
+ def post_params
+ params.require(:post).permit(:content, :publish_date)
+ end
+
+ def find_current_post
+ @post = Post.where(id: params[:id]).take
+ end
+
+end
diff --git a/app/controllers/user_relations_controller.rb b/app/controllers/user_relations_controller.rb
new file mode 100644
index 0000000..7439500
--- /dev/null
+++ b/app/controllers/user_relations_controller.rb
@@ -0,0 +1,45 @@
+class UserRelationsController < ApplicationController
+before_action :find_current_user_relation, only: [:show, :destroy]
+before_filter :authenticate_user!
+
+ def index
+
+ end
+
+ def show
+
+ end
+
+
+ def new
+ @user_relation = UserRelation.new
+ end
+
+ def create
+ @user_relation = CommunityMembership.new(user_relation_params)
+ @user_relation.user_owner_id = current_user.id
+ if @user_relation.save
+ rednder :show
+ else
+ render :new
+ end
+ end
+
+ def destroy
+ @user_relation.destroy
+ redirect_to user_root_path
+ end
+
+ private
+
+
+ def user_relation_params
+ params.require(:community_membership).permit(:user_rel_id)
+ end
+
+
+ def find_current_user_relation
+ @user_relation = UserRelation.where(id: params[:id]).take
+ end
+
+end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 59e9eab..b02f8c6 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1,4 +1,40 @@
class UsersController < ApplicationController
+
def profile
end
+
+ def show_profile
+ if current_user == nil
+ redirect_to new_user_session_path
+ return
+ end
+
+ user = User.where(id: current_user.id).take
+ if user == nil
+ redirect_to root_path
+ return
+ end
+
+ if user.person?
+ person = Person.where(user_id: user.id).take
+ if person == nil
+ redirect_to url_for(:controller => :people, :action => :new)
+ else
+ redirect_to url_for(:controller => :people, :action => :show, :id => person.id)
+ end
+
+ elsif user.community?
+ community = Community.where(user_id: user.id).take
+ if community == nil
+ redirect_to url_for(:controller => :communities, :action => :new)
+ else
+ redirect_to url_for(:controller => :communities, :action => :show, :id =>community.id)
+ end
+ else
+ redirect_to root_path
+ end
+
+
+ end
+
end
diff --git a/app/models/community.rb b/app/models/community.rb
index 454db8b..372fe6c 100644
--- a/app/models/community.rb
+++ b/app/models/community.rb
@@ -1,4 +1,9 @@
class Community < ActiveRecord::Base
- has_one :user, :foreign_key => "id"
- has_many :users, through: :community_memberships, dependent: :destroy
+ belongs_to :user, :foreign_key => "user_id"
+ has_many :users, through: :community_memberships#, dependent: :destroy
+ has_many :community_memberships, dependent: :destroy
+
+ def user
+ User.where(id: self.user_id).take
+ end
end
diff --git a/app/models/community_membership.rb b/app/models/community_membership.rb
index 050c12b..60adf5b 100644
--- a/app/models/community_membership.rb
+++ b/app/models/community_membership.rb
@@ -1,4 +1,12 @@
class CommunityMembership < ActiveRecord::Base
- belongs_to :user
- belongs_to :community
+ belongs_to :user, :foreign_key => "user_id"
+ belongs_to :community, :foreign_key => "community_id"
+
+ def user
+ User.where(id: self.user_id).take
+ end
+
+ def community
+ Community.where(id: self.community_id).take
+ end
end
diff --git a/app/models/person.rb b/app/models/person.rb
index 5352276..5380967 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -1,3 +1,8 @@
class Person < ActiveRecord::Base
- has_one :user, :foreign_key => "id"
+ belongs_to :user, :foreign_key => "user_id"
+
+
+ def user
+ User.where(id: self.user_id).take
+ end
end
diff --git a/app/models/post.rb b/app/models/post.rb
index 542c073..a899fb9 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1,3 +1,7 @@
class Post < ActiveRecord::Base
- belongs_to :user
+ belongs_to :user, :foreign_key => "user_id"
+
+ def user
+ User.where(id: self.user_id).take
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index b5e8f87..e11e1c8 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -7,12 +7,38 @@ class User < ActiveRecord::Base
:rememberable,
:trackable,
:validatable
-#has_many :communications
-#has_many :posts
- belongs_to :person, dependent: :destroy
- belongs_to :community, dependent: :destroy
- has_many :community, through: :community_memberships, dependent: :destroy
- has_many :user_owner_relations, :class_name =>'UserRelation', :foreign_key =>"owner_id", :inverse_of => :user_owner, dependent: :destroy
+ has_one :person, dependent: :destroy
+ has_one :community, dependent: :destroy
+ has_many :community_memberships, dependent: :destroy
+ has_many :user_owner_relations, :class_name =>'UserRelation', :foreign_key =>"user_owner_id", :inverse_of => :user_owner, dependent: :destroy
has_many :user_rel_relations, :class_name => 'UserRelation', :foreign_key =>"user_rel_id", :inverse_of => :user_rel, dependent: :destroy
has_many :posts, dependent: :destroy
+
+ def kind_name
+ if self.kind == 1
+ 'Person'
+ elsif self.kind == 2
+ 'Community'
+ else
+ 'Unknown kind'
+ end
+ end
+
+ def person?
+ self.kind == 1
+ end
+
+ def community?
+ self.kind == 2
+ end
+
+# def person_id
+# person = Person.where(user_id: self.id).take
+# if person == nil
+# nil
+# else
+# person.id
+# end
+# end
+
end
diff --git a/app/models/user_relation.rb b/app/models/user_relation.rb
index d07b78c..8c63dd2 100644
--- a/app/models/user_relation.rb
+++ b/app/models/user_relation.rb
@@ -3,4 +3,12 @@ class UserRelation < ActiveRecord::Base
validates :user_owner, :presence => true
belongs_to :user_rel, :class_name => "User", :inverse_of => :user_rel_relations
+
+ def user
+ User.where(id: self.user_owner_id).take
+ end
+
+ def user_rel
+ User.where(id: self.user_rel_id).take
+ end
end
diff --git a/app/views/communities/_form.html.erb b/app/views/communities/_form.html.erb
new file mode 100644
index 0000000..8a68ac1
--- /dev/null
+++ b/app/views/communities/_form.html.erb
@@ -0,0 +1,15 @@
+
+<%= form_for(@community) do |f| %>
+<%= render 'shared/communities_errors' %>
+
+
+ <%= f.label :subject %>
+ <%= f.text_field :subject, autofocus: true, autocomplete: "on"%>
+
+
+
+
+ <%= f.submit "Save" %>
+
+
+<% end %>
diff --git a/app/views/communities/edit.html.erb b/app/views/communities/edit.html.erb
new file mode 100644
index 0000000..16fa0e1
--- /dev/null
+++ b/app/views/communities/edit.html.erb
@@ -0,0 +1,3 @@
+Edit community data
+
+<%= render "form" %>
diff --git a/app/views/communities/index.html.erb b/app/views/communities/index.html.erb
new file mode 100644
index 0000000..d7d679d
--- /dev/null
+++ b/app/views/communities/index.html.erb
@@ -0,0 +1 @@
+Hello? index comm!
\ No newline at end of file
diff --git a/app/views/communities/new.html.erb b/app/views/communities/new.html.erb
new file mode 100644
index 0000000..9a8fd59
--- /dev/null
+++ b/app/views/communities/new.html.erb
@@ -0,0 +1,2 @@
+Create community data
+<%= render "form" %>
diff --git a/app/views/communities/show.html.erb b/app/views/communities/show.html.erb
new file mode 100644
index 0000000..7f24a58
--- /dev/null
+++ b/app/views/communities/show.html.erb
@@ -0,0 +1,11 @@
+<% content_for :title do %>
+ Social: Community profile
+<% end %>
+
+<%= render 'shared/user_info' %>
+
+
+
+Название: <%= current_user.name %>
+Тема: <%= @community.subject %>
+<%= link_to 'Изменить данные сообщества', edit_community_path %>
diff --git a/app/views/community_memberships/_form.html.erb b/app/views/community_memberships/_form.html.erb
new file mode 100644
index 0000000..6635cea
--- /dev/null
+++ b/app/views/community_memberships/_form.html.erb
@@ -0,0 +1,14 @@
+
+<%= form_for(@community_membership) do |f| %>
+
+
+ <%= f.label :user_id %>
+ <%= f.text_field :user_id, autofocus: true, autocomplete: "off"%>
+
+
+
+
+ <%= f.submit "Save" %>
+
+
+<% end %>
diff --git a/app/views/community_memberships/index.html.erb b/app/views/community_memberships/index.html.erb
new file mode 100644
index 0000000..e22fab5
--- /dev/null
+++ b/app/views/community_memberships/index.html.erb
@@ -0,0 +1 @@
+Hello index community membership!
diff --git a/app/views/community_memberships/new.html.erb b/app/views/community_memberships/new.html.erb
new file mode 100644
index 0000000..603cb48
--- /dev/null
+++ b/app/views/community_memberships/new.html.erb
@@ -0,0 +1,2 @@
+Create community membership data
+<%= render "form" %>
diff --git a/app/views/community_memberships/show.html.erb b/app/views/community_memberships/show.html.erb
new file mode 100644
index 0000000..5e0797f
--- /dev/null
+++ b/app/views/community_memberships/show.html.erb
@@ -0,0 +1,5 @@
+Сообщество: <%= @community_membership.community.subject %>
+Пользователь: <%= @community_membership.user.name %>
+Добавлен: <%= @community_membership.create_at %>
+
+
diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb
index 3ea40f0..d7aea1a 100644
--- a/app/views/devise/registrations/edit.html.erb
+++ b/app/views/devise/registrations/edit.html.erb
@@ -8,6 +8,11 @@
<%= f.email_field :email, autofocus: true %>
+
+ <%= f.label :name %>
+ <%= f.text_field :name, autocomplete: "on" %>
+
+
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% end %>
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb
index 343b265..a7f9ed4 100644
--- a/app/views/devise/registrations/new.html.erb
+++ b/app/views/devise/registrations/new.html.erb
@@ -8,6 +8,18 @@
<%= f.email_field :email, autofocus: true %>
+
+ <%= f.label :name %>
+ <%= f.text_field :name, autocomplete: "on" %>
+
+
+
+
+ <%= f.label :kind %>
+ <%= f.radio_button :kind, '1', :checked => true %><%= f.label :kind, "User" %>
+ <%= f.radio_button :kind, '2' %><%= f.label :kind, "Community" %>
+
+
<%= f.label :password %>
<% if @validatable %>
@@ -21,6 +33,8 @@
<%= f.password_field :password_confirmation, autocomplete: "off" %>
+
+
<%= f.submit "Sign up" %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index c2d3b91..2f03a0e 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -51,8 +51,10 @@
<%if user_signed_in? %>
+
- <%= link_to "My profile", user_root_path %>
- <%= link_to "News", "/" %>
+ - <%= link_to "Search", "/" %>
- <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<%else%>
- <%= link_to "Log in", new_user_session_path %>
diff --git a/app/views/people/_form.html.erb b/app/views/people/_form.html.erb
new file mode 100644
index 0000000..0f3654b
--- /dev/null
+++ b/app/views/people/_form.html.erb
@@ -0,0 +1,20 @@
+
+<%= form_for(@person) do |f| %>
+<%= render 'shared/people_errors' %>
+
+
+ <%= f.label :surename %>
+ <%= f.text_field :surename, autofocus: true, autocomplete: "on"%>
+
+
+
+
+ <%= f.label :date_of_birth %>
+ <%= f.date_select :date_of_birth %>
+
+
+
+ <%= f.submit "Save" %>
+
+
+<% end %>
diff --git a/app/views/people/edit.html.erb b/app/views/people/edit.html.erb
new file mode 100644
index 0000000..9365fd9
--- /dev/null
+++ b/app/views/people/edit.html.erb
@@ -0,0 +1,3 @@
+Edit person data
+
+<%= render "form" %>
diff --git a/app/views/people/index.html.erb b/app/views/people/index.html.erb
new file mode 100644
index 0000000..ec7d200
--- /dev/null
+++ b/app/views/people/index.html.erb
@@ -0,0 +1 @@
+Hello index person!
\ No newline at end of file
diff --git a/app/views/people/new.html.erb b/app/views/people/new.html.erb
new file mode 100644
index 0000000..22dfa27
--- /dev/null
+++ b/app/views/people/new.html.erb
@@ -0,0 +1,2 @@
+Create person data
+<%= render "form" %>
diff --git a/app/views/people/show.html.erb b/app/views/people/show.html.erb
new file mode 100644
index 0000000..7fdaf3e
--- /dev/null
+++ b/app/views/people/show.html.erb
@@ -0,0 +1,14 @@
+<% content_for :title do %>
+ Social: My profile
+<% end %>
+
+<%= render 'shared/user_info' %>
+
+
+
+Имя: <%= current_user.name %>
+Фамилия: <%= @person.surename %>
+Дата рождения: <%= @person.date_of_birth %>
+<%= link_to 'Изменить данные пользователя', edit_person_path %>
+
+
diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb
new file mode 100644
index 0000000..54cd64a
--- /dev/null
+++ b/app/views/posts/_form.html.erb
@@ -0,0 +1,19 @@
+
+<%= form_for(@post) do |f| %>
+
+
+ <%= f.label :content %>
+ <%= f.text_field :content, autofocus: true, autocomplete: "off"%>
+
+
+
+
+ <%= f.label :publish_date %>
+ <%= f.date_select :publish_date %>
+
+
+
+ <%= f.submit "Save" %>
+
+
+<% end %>
diff --git a/app/views/posts/edit.html.erb b/app/views/posts/edit.html.erb
new file mode 100644
index 0000000..ac93df2
--- /dev/null
+++ b/app/views/posts/edit.html.erb
@@ -0,0 +1,3 @@
+Edit post data
+
+<%= render "form" %>
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb
new file mode 100644
index 0000000..42ff2ad
--- /dev/null
+++ b/app/views/posts/index.html.erb
@@ -0,0 +1 @@
+Hello index post!
diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb
new file mode 100644
index 0000000..42bf8fb
--- /dev/null
+++ b/app/views/posts/new.html.erb
@@ -0,0 +1,2 @@
+Create post data
+<%= render "form" %>
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
new file mode 100644
index 0000000..c4403ac
--- /dev/null
+++ b/app/views/posts/show.html.erb
@@ -0,0 +1,5 @@
+Текущий пользователь: <%= @post.user.name %>
+содержание: <%= @post.content %>
+Дата публикации: <%= @post.publish_date %>
+
+
diff --git a/app/views/shared/_communities_errors.html.erb b/app/views/shared/_communities_errors.html.erb
new file mode 100644
index 0000000..e69de29
diff --git a/app/views/shared/_people_errors.html.erb b/app/views/shared/_people_errors.html.erb
new file mode 100644
index 0000000..e69de29
diff --git a/app/views/shared/_user_info.html.erb b/app/views/shared/_user_info.html.erb
new file mode 100644
index 0000000..d215f49
--- /dev/null
+++ b/app/views/shared/_user_info.html.erb
@@ -0,0 +1,12 @@
+<% if user_signed_in? %>
+ Здравствуйте, <%= current_user.name %>
+ Тип учетной записи: <%= current_user.kind_name %>
+ <%= link_to 'Изменить данные учетной записи', edit_user_registration_path %>
+
+
+
+
+
+<% else %>
+ <%= link_to 'Войти', new_user_session_path %> или <%= link_to 'Зарегистрироваться', new_user_registration_path %>
+<% end %>
diff --git a/app/views/user_relations/_form.html.erb b/app/views/user_relations/_form.html.erb
new file mode 100644
index 0000000..a8505be
--- /dev/null
+++ b/app/views/user_relations/_form.html.erb
@@ -0,0 +1,14 @@
+
+<%= form_for(@user_relation) do |f| %>
+
+
+ <%= f.label :user_rel_id %>
+ <%= f.text_field :user_rel_id, autofocus: true, autocomplete: "off"%>
+
+
+
+
+ <%= f.submit "Save" %>
+
+
+<% end %>
diff --git a/app/views/user_relations/index.html.erb b/app/views/user_relations/index.html.erb
new file mode 100644
index 0000000..77e45ab
--- /dev/null
+++ b/app/views/user_relations/index.html.erb
@@ -0,0 +1 @@
+Hello index user relation!
diff --git a/app/views/user_relations/new.html.erb b/app/views/user_relations/new.html.erb
new file mode 100644
index 0000000..8e8f824
--- /dev/null
+++ b/app/views/user_relations/new.html.erb
@@ -0,0 +1,2 @@
+Create user relation data
+<%= render "form" %>
diff --git a/app/views/user_relations/show.html.erb b/app/views/user_relations/show.html.erb
new file mode 100644
index 0000000..7098d4a
--- /dev/null
+++ b/app/views/user_relations/show.html.erb
@@ -0,0 +1,5 @@
+Текущий пользователь: <%= @user_relation.user.name %>
+Пользователь-друг: <%= @user_relation.user_rel.name %>
+Добавлен: <%= @user_relation.create_at %>
+
+
diff --git a/app/views/users/_community.html.erb b/app/views/users/_community.html.erb
new file mode 100644
index 0000000..c0afad8
--- /dev/null
+++ b/app/views/users/_community.html.erb
@@ -0,0 +1 @@
+Community
\ No newline at end of file
diff --git a/app/views/users/_person.html.erb b/app/views/users/_person.html.erb
new file mode 100644
index 0000000..579bc25
--- /dev/null
+++ b/app/views/users/_person.html.erb
@@ -0,0 +1 @@
+Person data
\ No newline at end of file
diff --git a/app/views/users/profile.html.erb b/app/views/users/profile.html.erb
index ee16858..aae312b 100644
--- a/app/views/users/profile.html.erb
+++ b/app/views/users/profile.html.erb
@@ -1,6 +1,22 @@
+<% content_for :title do %>
+ Social: My profile
+<% end %>
+
<% if user_signed_in? %>
- Здравствуйте, <%= current_user.email %>
- <%= link_to 'Выйти', destroy_user_session_path, :method => :delete %>
+ Здравствуйте, <%= current_user.name %>
+ Тип учетной записи: <%= current_user.kind_name %>
+ <%= link_to 'Изменить данные учетной записи', edit_user_registration_path %>
+
+ <% if current_user.person? %>
+ <%= render "person" %>
+ <% end %>
+
+ <% if current_user.community? %>
+ <%= render "community" %>
+ <% end %>
+
+
+
<% else %>
<%= link_to 'Войти', new_user_session_path %> или <%= link_to 'Зарегистрироваться', new_user_registration_path %>
<% end %>
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index dd5dcdb..605a966 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,7 +2,13 @@
#get 'users/profile'
devise_for :users
+ resources :people
+ resources :communities
+ resources :community_memberships
+ resources :user_relations
+ resources :posts
+ # resorces :users, only:[:show, :index]
#get 'welcome/index'
root 'welcome#index'
- get 'users/profile', as: 'user_root'
+ get 'user_root' => 'users#show_profile'
end
diff --git a/spec/controllers/communities_controller_spec.rb b/spec/controllers/communities_controller_spec.rb
new file mode 100644
index 0000000..96f62d7
--- /dev/null
+++ b/spec/controllers/communities_controller_spec.rb
@@ -0,0 +1,75 @@
+require 'rails_helper'
+
+RSpec.describe CommunitiesController, :type => :controller do
+
+ context 'when user not logged in' do
+ let(:community) {FactoryGirl.create(:community)}
+
+ describe "Get community_profile" do
+ it 'redirect to login page' do
+ get :show, :id => community.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+ describe "Edit community_profile" do
+ it 'redirect to login page' do
+ get :edit, :id => community.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ describe "New community_profile" do
+ it 'redirect to login page' do
+ get :new
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ end
+
+
+ context 'when user logged in as community with community data' do
+ let(:community) {FactoryGirl.create(:community)}
+
+ before do
+ sign_in community.user
+
+ end
+
+ describe "GET community profile"
+ it 'get community profile' do
+ get :show, :id => community.id
+ expect(response).to render_template :show
+ end
+
+ describe "GET new community"
+ it 'assigns new community' do
+ get :new
+ expect(assigns(:community)).to be_new_record
+ end
+
+ describe "render new community"
+ it 'render new community ' do
+ get :new
+ expect(response).to render_template :new
+ end
+
+ describe "GET #edit" do
+ it "edit community data" do
+ get :edit, id: community
+ expect(assigns(:community)).to eq(community)
+ end
+
+ it "renders the :edit view" do
+ get :edit, id: community
+ expect(response).to render_template :edit
+ end
+ end
+
+
+
+ end
+
+end
diff --git a/spec/controllers/community_memberships_controller_spec.rb b/spec/controllers/community_memberships_controller_spec.rb
new file mode 100644
index 0000000..5962342
--- /dev/null
+++ b/spec/controllers/community_memberships_controller_spec.rb
@@ -0,0 +1,77 @@
+require 'rails_helper'
+
+RSpec.describe CommunityMembershipsController, :type => :controller do
+
+ context 'when user not logged in' do
+ let(:community_membership) {FactoryGirl.create(:community_membership)}
+
+ describe "Get community_membership" do
+ it 'redirect to login page' do
+ get :show, :id => community_membership.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+
+ describe "New community_membership" do
+ it 'redirect to login page' do
+ get :new
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+ describe "Delete community_membership" do
+ it 'redirect to login page' do
+ delete :destroy, id: community_membership
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ end
+
+context 'when user logged in as community with community data' do
+ let(:community_membership) {FactoryGirl.create(:community_membership)}
+
+ before do
+ sign_in community_membership.community.user
+
+ end
+
+ describe "GET community_membership profile"
+ it 'get community_membership profile' do
+ get :show, :id => community_membership.id
+ expect(response).to render_template :show
+ end
+
+ describe "GET new community_membership"
+ it 'assigns new community_membership' do
+ get :new
+ expect(assigns(:community_membership)).to be_new_record
+ end
+
+ describe "render new community_membership"
+ it 'render new community_membership ' do
+ get :new
+ expect(response).to render_template :new
+ end
+
+ describe 'DELETE #destroy' do
+ before(:each) {
+ @community_membership = FactoryGirl.create :community_membership
+ }
+ it "deletes the community_membership" do
+ expect {delete :destroy, id: @community_membership}.to change(CommunityMembership, :count).by(-1)
+ end
+
+ it "redirects to community" do
+ community_id = @community_membership.community_id
+ delete :destroy, id: @community_membership
+ expect(response).to redirect_to :controller => :communities, :action => :show, :id => community_id
+ end
+ end#
+
+ end
+
+end
diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb
new file mode 100644
index 0000000..7bb80e6
--- /dev/null
+++ b/spec/controllers/people_controller_spec.rb
@@ -0,0 +1,74 @@
+require 'rails_helper'
+
+RSpec.describe PeopleController, :type => :controller do
+
+ context 'when user not logged in' do
+ let(:person) {FactoryGirl.create(:person)}
+
+ describe "Get people_profile" do
+ it 'redirect to login page' do
+ get :show, :id => person.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+ describe "Edit people_profile" do
+ it 'redirect to login page' do
+ get :edit, :id => person.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ describe "New people_profile" do
+ it 'redirect to login page' do
+ get :new
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ end
+
+
+ context 'when user logged in as person with person data' do
+ let(:person) {FactoryGirl.create(:person)}
+
+ before do
+ sign_in person.user
+
+ end
+
+ describe "GET person profile"
+ it 'get person profile' do
+ get :show, :id => person.id
+ expect(response).to render_template :show
+ end
+
+ describe "GET new person"
+ it 'assigns new person' do
+ get :new
+ expect(assigns(:person)).to be_new_record
+ end
+
+ describe "render new person"
+ it 'render new person ' do
+ get :new
+ expect(response).to render_template :new
+ end
+
+ describe "GET #edit" do
+ it "edit person data" do
+ get :edit, id: person
+ expect(assigns(:person)).to eq(person)
+ end
+
+ it "renders the :edit view" do
+ get :edit, id: person
+ expect(response).to render_template :edit
+ end
+ end
+
+ end
+
+
+end
diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb
new file mode 100644
index 0000000..f6689fd
--- /dev/null
+++ b/spec/controllers/posts_controller_spec.rb
@@ -0,0 +1,94 @@
+require 'rails_helper'
+
+RSpec.describe PostsController, :type => :controller do
+
+context 'when user not logged in' do
+ let(:post) {FactoryGirl.create(:post)}
+
+ describe "Get post" do
+ it 'redirect to login page' do
+ get :show, :id => post.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+
+ describe "New post" do
+ it 'redirect to login page' do
+ get :new
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ describe "Edit post" do
+ it 'redirect to login page' do
+ get :edit, :id => post.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ describe "Delete post" do
+ it 'redirect to login page' do
+ delete :destroy, id: post
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ end
+
+ context 'when user logged in ' do
+ let(:post) {FactoryGirl.create(:post)}
+
+ before do
+ sign_in post.user
+
+ end
+
+ describe "GET post profile"
+ it 'get post profile' do
+ get :show, :id => post.id
+ expect(response).to render_template :show
+ end
+
+ describe "GET new post"
+ it 'assigns new post' do
+ get :new
+ expect(assigns(:post)).to be_new_record
+ end
+
+ describe "render new post"
+ it 'render new post ' do
+ get :new
+ expect(response).to render_template :new
+ end
+
+ describe "GET #edit" do
+ it "edit post data" do
+ get :edit, id: post
+ expect(assigns(:post)).to eq(post)
+ end
+
+ it "renders the :edit view" do
+ get :edit, id: post
+ expect(response).to render_template :edit
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ before(:each) {
+ @post = FactoryGirl.create :post
+ }
+ it "deletes the post" do
+ expect {delete :destroy, id: @post}.to change(Post, :count).by(-1)
+ end
+
+ it "redirects to user_profile" do
+ delete :destroy, id: @post
+ expect(response).to redirect_to user_root_path
+ end
+ end#
+
+ end
+
+end
diff --git a/spec/controllers/user_relations_controller_spec.rb b/spec/controllers/user_relations_controller_spec.rb
new file mode 100644
index 0000000..f756a42
--- /dev/null
+++ b/spec/controllers/user_relations_controller_spec.rb
@@ -0,0 +1,76 @@
+require 'rails_helper'
+
+RSpec.describe UserRelationsController, :type => :controller do
+
+context 'when user not logged in' do
+ let(:user_relation) {FactoryGirl.create(:user_relation)}
+
+ describe "Get user_relation" do
+ it 'redirect to login page' do
+ get :show, :id => user_relation.id
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+
+ describe "New user_relation" do
+ it 'redirect to login page' do
+ get :new
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+
+ describe "Delete user_relation" do
+ it 'redirect to login page' do
+ delete :destroy, id: user_relation
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+
+ end
+
+ context 'when user logged in ' do
+ let(:user_relation) {FactoryGirl.create(:user_relation)}
+
+ before do
+ sign_in user_relation.user
+
+ end
+
+ describe "GET user_relation profile"
+ it 'get user_relation profile' do
+ get :show, :id => user_relation.id
+ expect(response).to render_template :show
+ end
+
+ describe "GET new user_relation"
+ it 'assigns new user_relation' do
+ get :new
+ expect(assigns(:user_relation)).to be_new_record
+ end
+
+ describe "render new user_relation"
+ it 'render new user_relation ' do
+ get :new
+ expect(response).to render_template :new
+ end
+
+ describe 'DELETE #destroy' do
+ before(:each) {
+ @user_relation = FactoryGirl.create :user_relation
+ }
+ it "deletes the user_relation" do
+ expect {delete :destroy, id: @user_relation}.to change(UserRelation, :count).by(-1)
+ end
+
+ it "redirects to user_profile" do
+ delete :destroy, id: @user_relation
+ expect(response).to redirect_to user_root_path
+ end
+ end#
+
+ end
+
+end
diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb
index 951bc7d..f8025ab 100644
--- a/spec/controllers/users_controller_spec.rb
+++ b/spec/controllers/users_controller_spec.rb
@@ -2,11 +2,84 @@
RSpec.describe UsersController, :type => :controller do
- describe "GET profile" do
- it "returns http success" do
- get :profile
- expect(response).to be_success
+
+ context 'when user not logged in' do
+ describe "GET user_profile" do
+ it 'redirect to login page' do
+ get :show_profile
+ expect(response).to redirect_to new_user_session_path
+ end
+ end
+ end
+
+
+ context 'when user logged in as person, but no person data' do
+ let(:user) {FactoryGirl.create(:user_person)}
+
+ before do
+ sign_in user
+
+ end
+
+ describe "GET user_profile if user"
+ it 'redirect person#new view' do
+ get :show_profile
+ expect(response).to redirect_to new_person_path
+ end
+
+ end
+
+ context 'when user logged in as person with person data' do
+ let(:person) {FactoryGirl.create(:person)}
+
+ before do
+ sign_in person.user
+
end
+
+ describe "GET user_profile if user"
+ it 'redirect person#show view' do
+ get :show_profile
+ expect(response).to redirect_to :controller => :people, :action => :show, :id => person.id
+ end
+
end
+
+
+
+ context 'when user logged in as community, but no community data' do
+ let(:user) {FactoryGirl.create(:user_community)}
+
+ before do
+ sign_in user
+
+ end
+
+ describe "GET user_profile if community"
+ it 'redirect community#new view' do
+ get :show_profile
+ expect(response).to redirect_to new_community_path
+ end
+
+ end
+
+ context 'when user logged in as community with community data' do
+ let(:community) {FactoryGirl.create(:community)}
+
+ before do
+ sign_in community.user
+
+ end
+
+ describe "GET community_profile if user"
+ it 'redirect person#show view' do
+ get :show_profile
+ expect(response).to redirect_to :controller => :communities, :action => :show, :id => community.id
+ end
+
+ end
+
+
+
end
diff --git a/spec/factories/communities.rb b/spec/factories/communities.rb
index 4000502..316ad24 100644
--- a/spec/factories/communities.rb
+++ b/spec/factories/communities.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :community do
subject {Faker::Lorem.sentence}
- association :user
+ association :user, factory: :user_community
end
end
diff --git a/spec/factories/community_memberships.rb b/spec/factories/community_memberships.rb
index b3a78e7..8d5f820 100644
--- a/spec/factories/community_memberships.rb
+++ b/spec/factories/community_memberships.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :community_membership do
- association :community
- association :user
+ association :community, factory: :community
+ association :user, factory: :user
end
end
diff --git a/spec/factories/people.rb b/spec/factories/people.rb
index bda01fb..9ddd27e 100644
--- a/spec/factories/people.rb
+++ b/spec/factories/people.rb
@@ -2,7 +2,7 @@
factory :person do
surename {Faker::Name.last_name}
date_of_birth {Faker::Time.between(10000.days.ago,10000.days.ago)}
- association :user
+ association :user, factory: :user_person
end
end
diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb
index f4ae1d4..1cf964b 100644
--- a/spec/factories/posts.rb
+++ b/spec/factories/posts.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :post do
content {Faker::Lorem.paragraph}
- association :user
+ association :user, factory: :user
end
end
diff --git a/spec/factories/user_relations.rb b/spec/factories/user_relations.rb
index 2d83e2e..aeb778e 100644
--- a/spec/factories/user_relations.rb
+++ b/spec/factories/user_relations.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :user_relation do
- association :user_owner
- association :user_rel
+ association :user_owner, factory: :user
+ association :user_rel, factory: :user
end
diff --git a/spec/factories/users.rb b/spec/factories/users.rb
index 956d741..9321d8d 100644
--- a/spec/factories/users.rb
+++ b/spec/factories/users.rb
@@ -3,7 +3,7 @@
name {Faker::Name.name}
email {Faker::Internet.email}
password {Faker::Internet.password(20)}
- kind {Faker::Number.number(1)}
+ kind {Faker::Number.number(2)}
end
@@ -11,7 +11,7 @@
name {Faker::Name.name}
email {Faker::Internet.email}
password {Faker::Internet.password(20)}
- kind {Faker::Number.number(1)}
+ kind {Faker::Number.number(2)}
end
@@ -19,8 +19,22 @@
name {Faker::Name.name}
email {Faker::Internet.email}
password {Faker::Internet.password(20)}
- kind {Faker::Number.number(1)}
+ kind {Faker::Number.number(2)}
end
+ factory :user_person, :class => User do
+ name {Faker::Name.name}
+ email {Faker::Internet.email}
+ password {Faker::Internet.password(20)}
+ kind {1}
+ end
+
+
+ factory :user_community, :class => User do
+ name {Faker::Name.name}
+ email {Faker::Internet.email}
+ password {Faker::Internet.password(20)}
+ kind {2}
+ end
end
diff --git a/spec/models/community_spec.rb b/spec/models/community_spec.rb
index 83a2878..c55edb3 100644
--- a/spec/models/community_spec.rb
+++ b/spec/models/community_spec.rb
@@ -5,5 +5,5 @@
expect(FactoryGirl.build(:community)).to be_valid
end
- it {should have_one(:user).class_name(User)}
+ it {should belong_to(:user).class_name(User)}
end
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index 9bd0eb9..f57be25 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -5,6 +5,6 @@
expect(FactoryGirl.build(:person)).to be_valid
end
- it {should have_one(:user).class_name(User)}
+ it {should belong_to(:user).class_name(User)}
end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 4d1d439..887c4df 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -40,4 +40,6 @@
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
+
+ config.include Devise::TestHelpers, :type => :controller
end