aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKir2011-10-31 21:43:34 +0400
committerKir2011-10-31 21:43:34 +0400
commit48c144ed34e2d5977bb8e5af8a9c7e8dfc253361 (patch)
treea672937fb96b955f3c2d4364195926b3b14d01cb
parent3ddd221f5f235e34afb9d4bbc7a4fc05228f73a4 (diff)
downloadinboxes-48c144ed34e2d5977bb8e5af8a9c7e8dfc253361.tar.bz2
Small changed
-rw-r--r--app/controllers/discussions_controller.rb42
-rw-r--r--app/controllers/messages_controller.rb18
-rw-r--r--app/models/discussion.rb26
-rw-r--r--app/models/discussion_view.rb7
-rw-r--r--app/models/message.rb10
-rw-r--r--app/views/discussions/_form.html.haml2
-rw-r--r--app/views/discussions/index.html.haml8
-rw-r--r--app/views/discussions/new.html.haml3
-rw-r--r--app/views/discussions/show.html.haml14
-rw-r--r--app/views/messages/_form.html.haml6
-rw-r--r--config/locales/en.yml7
-rw-r--r--config/locales/ru.yml7
-rw-r--r--lib/generators/inboxes/views_generator.rb25
-rw-r--r--lib/inboxes.rb26
-rw-r--r--lib/inboxes/railtie.rb24
15 files changed, 165 insertions, 60 deletions
diff --git a/app/controllers/discussions_controller.rb b/app/controllers/discussions_controller.rb
index 7d5d8ca..8ea6459 100644
--- a/app/controllers/discussions_controller.rb
+++ b/app/controllers/discussions_controller.rb
@@ -1,6 +1,8 @@
class DiscussionsController < ApplicationController
- # load_and_authorize_resource
- # before_filter :load_and_check_discussion_recipient, :only => [:create, :new]
+ before_filter :authenticate_user!
+ before_filter :check_permissions, :only => :show
+
+ before_filter :load_and_check_discussion_recipient, :only => [:create, :new]
def index
# показываем дискуссии юзера, и те куда его присоеденили
@@ -17,53 +19,46 @@ class DiscussionsController < ApplicationController
# GET /discussions/1.json
def show
@discussion = Discussion.includes(:messages, :speakers).find(params[:id])
- # @discussion.mark_as_read_for(current_user) # сделаем прочтенной для пользователя
+ redirect_to root_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user)
- # члены дискуссии - приглашенные в нее + ее создатель
- @message = Message.new
-
- respond_to do |format|
- format.html # show.html.erb
- format.json { render :json => @discussion }
- end
+ @discussion.mark_as_read_for(current_user) # сделаем прочтенной для пользователя
end
# GET /discussions/new
# GET /discussions/new.json
def new
- @discussion = Discussion.new
+ # @discussion = Discussion.new
@discussion.messages.build
end
# POST /discussions
# POST /discussions.json
def create
- @discussion = Discussion.new(params[:discussion])
+ # @discussion = Discussion.new(params[:discussion])
+ @discussion.add_recipient_token current_user.id
+
@discussion.messages.each do |m|
m.discussion = @discussion
m.user = current_user
end
- @discussion.add_recipient_token current_user.id
- respond_to do |format|
- if @discussion.save
- format.html { redirect_to @discussion, :notice => 'Дискуссия начата.' }
- # format.json { render :json => @discussion, :status => :created, :location => @discussion }
- else
- format.html { render :action => "new" }
- # format.json { render :json => @discussion.errors, :status => :unprocessable_entity }
- end
+
+ if @discussion.save
+ redirect_to @discussion, :notice => t("views.discussions.started")
+ else
+ render :action => "new"
end
end
def leave
@discussion.remove_speaker(current_user)
- redirect_to discussions_url, :notice => "Вы успешно покинули дискуссию."
+ redirect_to discussions_url, :notice => t("views.discussions.leaved")
end
private
def load_and_check_discussion_recipient
+ @discussion = Discussion.new((params[:discussion] ? params[:discussion] : {}))
@discussion.recipient_tokens = params[:recipients] if params[:recipients]
# проверка, существует ли уже дискуссия с этим человеком
@@ -76,9 +71,10 @@ class DiscussionsController < ApplicationController
Message.create!(:discussion => discussion, :user => current_user, :body => m.body) if m.body
end
# перекидываем на нее
- redirect_to discussion_url(discussion), :notice => "Переписка между вами уже существует."
+ redirect_to discussion_url(discussion), :notice => t("views.discussions.exists", :user => user[Inboxes::config.user_name])
end
end
end
+
end
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
new file mode 100644
index 0000000..a302237
--- /dev/null
+++ b/app/controllers/messages_controller.rb
@@ -0,0 +1,18 @@
+class MessagesController < ApplicationController
+
+ def create
+ @discussion = Discussion.find(params[:discussion_id])
+ redirect_to root_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user)
+
+ @message = Message.new(params[:message])
+ @message.user = current_user
+ @message.discussion = @discussion
+ @message.save
+
+ respond_to do |format|
+ format.html { redirect_to @message.discussion }
+ format.js
+ end
+ end
+
+end
diff --git a/app/models/discussion.rb b/app/models/discussion.rb
index 474ad00..7898140 100644
--- a/app/models/discussion.rb
+++ b/app/models/discussion.rb
@@ -66,15 +66,13 @@ class Discussion < ActiveRecord::Base
# проверяет, есть ли уже беседа между пользователями
# TODO вынести в отдельный метод а в этом возращать true/false, а то неправославно как-то
def self.find_between_users(user, user2)
- res = nil
+ dialog = nil
discussions = self.joins(:speakers).includes(:users).where("speakers.user_id = ?", user.id)
Rails.logger.info "Searching for ids: #{user.id}, #{user2.id}"
discussions.each do |discussion|
-
- res = discussion if discussion.private? && ((discussion.users.first == user && discussion.users.last == user2) || (discussion.users.first == user2 && discussion.users.last == user))
- Rails.logger.info "Searching for ids: #{discussion.users.inspect}" if discussion.private?
+ dialog = discussion if discussion.private? && ((discussion.users.first == user && discussion.users.last == user2) || (discussion.users.first == user2 && discussion.users.last == user))
end
- res
+ dialog
end
# приватная/групповая
@@ -88,14 +86,14 @@ class Discussion < ActiveRecord::Base
end
# проверка, является ли дискуссия непрочитанной для пользователя
- # def unread_for?(user)
- # flag = self.views.find_by_user_id(user.id)
- # if flag
- # self.updated_at >= flag.updated_at
- # else
- # true
- # end
- # end
+ def unread_for?(user)
+ flag = self.views.find_by_user_id(user.id)
+ if flag
+ self.updated_at >= flag.updated_at
+ else
+ true
+ end
+ end
# пометить как прочитанная
def mark_as_read_for(user)
@@ -112,7 +110,7 @@ class Discussion < ActiveRecord::Base
def check_that_has_at_least_two_users
Rails.logger.info self.recipient_ids
- errors.add :recipient_tokens, "Укажите хотя бы одного получателя" #if !self.recipient_ids || self.recipient_ids.size < 2
+ errors.add :recipient_tokens, "Укажите хотя бы одного получателя" if !self.recipient_ids || self.recipient_ids.size < 2
end
end \ No newline at end of file
diff --git a/app/models/discussion_view.rb b/app/models/discussion_view.rb
new file mode 100644
index 0000000..5714bb5
--- /dev/null
+++ b/app/models/discussion_view.rb
@@ -0,0 +1,7 @@
+class DiscussionView < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :discussion
+
+ validates :user, :discussion, :presence => true
+ validates_uniqueness_of :user_id, :scope => :discussion_id
+end
diff --git a/app/models/message.rb b/app/models/message.rb
index a38dfb0..b7f9b12 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -7,16 +7,16 @@ class Message < ActiveRecord::Base
validates :user, :discussion, :body, :presence => true
- # after_save :touch_discussion_and_mark_as_read
+ after_save :touch_discussion_and_mark_as_read
- # def visible_for? user
- # self.created_at.to_i >= self.discussion.user_invited_at(user).to_i
- # end
+ def visible_for? user
+ self.created_at.to_i >= self.discussion.user_invited_at(user).to_i
+ end
private
def touch_discussion_and_mark_as_read
self.discussion.touch
- # self.discussion.mark_as_read_for(self.user)
+ self.discussion.mark_as_read_for(self.user)
end
end
diff --git a/app/views/discussions/_form.html.haml b/app/views/discussions/_form.html.haml
index 52da4a6..eab0db6 100644
--- a/app/views/discussions/_form.html.haml
+++ b/app/views/discussions/_form.html.haml
@@ -1,7 +1,7 @@
= form_for @discussion do |f|
.entry
= f.label :recipient_tokens
- = f.collection_select :recipient_tokens, User.all, :id, :name, :prompt => true, :html_options => { :multiple => true }
+ = select_tag "discussion[recipient_tokens]", options_from_collection_for_select(User.all, :id, Inboxes::config.user_name), :multiple => true, :size => "4"
.entry
= f.fields_for :messages do |j|
= j.label :body
diff --git a/app/views/discussions/index.html.haml b/app/views/discussions/index.html.haml
index 669c01b..8f47198 100644
--- a/app/views/discussions/index.html.haml
+++ b/app/views/discussions/index.html.haml
@@ -1,5 +1,7 @@
-%h3 Discussions
+%h3 Discussions list
- @discussions.each do |discussion|
- = discussion.id
+ .discussion
+ = link_to "Discussion with #{discussion.users.collect{|u| u[Inboxes::config.user_name]}.join(', ')}", discussion
-= link_to "Create new", new_discussion_path \ No newline at end of file
+%p
+ = link_to "Create new", new_discussion_path \ No newline at end of file
diff --git a/app/views/discussions/new.html.haml b/app/views/discussions/new.html.haml
index 3f68ec9..ac6665c 100644
--- a/app/views/discussions/new.html.haml
+++ b/app/views/discussions/new.html.haml
@@ -1,2 +1,3 @@
%h3 New discussion
-= render "form" \ No newline at end of file
+= render "form"
+= link_to "All discussions", discussions_path \ No newline at end of file
diff --git a/app/views/discussions/show.html.haml b/app/views/discussions/show.html.haml
index 8c13728..903d8fd 100644
--- a/app/views/discussions/show.html.haml
+++ b/app/views/discussions/show.html.haml
@@ -1,5 +1,13 @@
-= @discussion.users.map { |u| u.name }.join(", ")
-= debug @discussion.speakers
+%h3
+ Chat with
+ = @discussion.users.map { |u| u[Inboxes::config.user_name] }.join(", ")
+%h3 Messages
- @discussion.messages.each do |message|
- = message.body \ No newline at end of file
+ %p
+ = "[#{message.user[Inboxes::config.user_name]}]"
+ = message.body
+ = "@ #{l(message.created_at)}"
+
+= render "messages/form"
+= link_to "All discussions", discussions_path \ No newline at end of file
diff --git a/app/views/messages/_form.html.haml b/app/views/messages/_form.html.haml
new file mode 100644
index 0000000..78ccaa0
--- /dev/null
+++ b/app/views/messages/_form.html.haml
@@ -0,0 +1,6 @@
+.message_form
+ = form_for Message.new, :url => discussion_messages_path(@discussion.id), :method => :post do |f|
+ = f.text_area :body
+ = f.submit "Отправить"
+
+= link_to "Выйти из переписки", leave_discussion_path(@discussion), :method => :post unless @discussion.private? \ No newline at end of file
diff --git a/config/locales/en.yml b/config/locales/en.yml
new file mode 100644
index 0000000..fa7803a
--- /dev/null
+++ b/config/locales/en.yml
@@ -0,0 +1,7 @@
+en:
+ views:
+ discussions:
+ started: "Discussion started"
+ leaved: "You leaved discussion"
+ exists: "Discussion between you and %{user} already exists"
+ can_not_participate: "You are not listed in this discussion" \ No newline at end of file
diff --git a/config/locales/ru.yml b/config/locales/ru.yml
new file mode 100644
index 0000000..06d3534
--- /dev/null
+++ b/config/locales/ru.yml
@@ -0,0 +1,7 @@
+ru:
+ views:
+ discussions:
+ started: "Чат начат"
+ leaved: "Вы покинули дискуссию"
+ exists: "Дискуссия между вами и %{user} уже существует"
+ can_not_participate: "Вы не состоите в этой дискуссии" \ No newline at end of file
diff --git a/lib/generators/inboxes/views_generator.rb b/lib/generators/inboxes/views_generator.rb
new file mode 100644
index 0000000..c69812e
--- /dev/null
+++ b/lib/generators/inboxes/views_generator.rb
@@ -0,0 +1,25 @@
+require 'rails/generators'
+
+module Inboxes
+ module Generators
+ class ViewsGenerator < Rails::Generators::Base
+ source_root File.expand_path('../../../../app/views', __FILE__)
+ class_option :template_engine, :type => :string, :aliases => '-e', :desc => 'Template engine for the views. Available options are "erb" and "haml".'
+
+ def copy_or_fetch
+ # puts "Generating views"
+ filename_pattern = File.join self.class.source_root, "*/*.html.#{template_engine}"
+ # puts (Dir.glob(filename_pattern).map {|f| File.basename f}).inspect
+ # Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f|
+ # copy_file f, "app/views/#{f}"
+ # end
+ end
+
+ private
+
+ def template_engine
+ options[:template_engine].try(:to_s).try(:downcase) || 'erb'
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/inboxes.rb b/lib/inboxes.rb
index 29bb078..9759f07 100644
--- a/lib/inboxes.rb
+++ b/lib/inboxes.rb
@@ -2,6 +2,26 @@ require "inboxes/version"
require "inboxes/railtie"
require "inboxes/engine"
-# module Inboxes
-# # Your code goes here...
-# end
+module Inboxes
+ # Your code goes here...
+
+ def self.configure(&block)
+ yield @config ||= Inboxes::Configuration.new
+ end
+
+ # Global settings for Inboxes
+ def self.config
+ @config
+ end
+
+ # need a Class for 3.0
+ class Configuration #:nodoc:
+ include ActiveSupport::Configurable
+ config_accessor :user_name
+
+ def param_name
+ config.param_name.respond_to?(:call) ? config.param_name.call() : config.param_name
+ end
+ end
+
+end
diff --git a/lib/inboxes/railtie.rb b/lib/inboxes/railtie.rb
index 5be6db5..06ab0d3 100644
--- a/lib/inboxes/railtie.rb
+++ b/lib/inboxes/railtie.rb
@@ -2,15 +2,25 @@ require 'rails'
module Inboxes
class Railtie < ::Rails::Railtie #:nodoc:
- initializer 'inboxes' do |app|
- ActiveSupport.on_load(:active_record) do
- # require 'kaminari/models/active_record_extension'
- # ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
+ config.inboxes = ActiveSupport::OrderedOptions.new
+
+ initializer "inboxes.configure" do |app|
+ Inboxes.configure do |config|
+ config.user_name = app.config.inboxes[:user_name] || "email"
end
- ActiveSupport.on_load(:action_view) do
- # ::ActionView::Base.send :include, Kaminari::ActionViewExtension
- end
+ # app.config.middleware.insert_before "::Rails::Rack::Logger", "Inboxes::Middleware"
end
+
+ # initializer 'inboxes' do |app|
+ # ActiveSupport.on_load(:active_record) do
+ # # require 'kaminari/models/active_record_extension'
+ # # ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
+ # end
+ #
+ # ActiveSupport.on_load(:action_view) do
+ # # ::ActionView::Base.send :include, Kaminari::ActionViewExtension
+ # end
+ # end
end
end \ No newline at end of file