diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | app/controllers/inboxes/base_controller.rb | 4 | ||||
| -rw-r--r-- | app/controllers/inboxes/discussions_controller.rb | 16 | ||||
| -rw-r--r-- | app/controllers/inboxes/messages_controller.rb | 6 | ||||
| -rw-r--r-- | app/controllers/inboxes/speakers_controller.rb | 4 | ||||
| -rw-r--r-- | app/models/discussion.rb | 12 | ||||
| -rw-r--r-- | app/models/inboxes/ability.rb | 6 | ||||
| -rw-r--r-- | app/models/message.rb | 12 | ||||
| -rw-r--r-- | app/models/speaker.rb | 10 | ||||
| -rw-r--r-- | app/views/inboxes/discussions/_form.html.haml | 2 | ||||
| -rw-r--r-- | app/views/inboxes/discussions/index.html.haml | 6 | ||||
| -rw-r--r-- | app/views/inboxes/discussions/show.html.haml | 6 | ||||
| -rw-r--r-- | config/routes.rb | 4 | ||||
| -rw-r--r-- | lib/generators/inboxes/install_generator.rb | 4 | ||||
| -rw-r--r-- | lib/generators/inboxes/templates/install.rb | 6 | ||||
| -rw-r--r-- | lib/generators/inboxes/views_generator.rb | 2 | ||||
| -rw-r--r-- | lib/inboxes.rb | 8 | ||||
| -rw-r--r-- | lib/inboxes/ability.rb | 6 | ||||
| -rw-r--r-- | lib/inboxes/active_record_extension.rb | 2 | ||||
| -rw-r--r-- | lib/inboxes/engine.rb | 2 | ||||
| -rw-r--r-- | lib/inboxes/railtie.rb | 4 |
21 files changed, 68 insertions, 62 deletions
@@ -20,7 +20,7 @@ Remember that unfortunately, Inboxes reserve 3 resources names: Discussion, Mess 1. Add `gem "inboxes", "~> 0.1.2"` to the `Gemfile` and run `bundle install` 2. Execute `rails generate inboxes:install`. This command will generate migration for messaging system. Don't forget to run migrations: `rake db:migrate` -3. Add `inboxes` to your User model like [here](https://gist.github.com/1330080) +3. Add `has_inboxes` to your User model like [here](https://gist.github.com/1330080) 4. Now Inboxes are ready to use. Open `http://yoursite.dev/discussions` to see the list of discussions. You can start new one. Default Inboxes views are ugly, so you can copy into your app and make anything with them: `rails generate inboxes:views` @@ -38,9 +38,9 @@ You can watch the demo of integration [on YouTube](http://youtu.be/c12gey9DvyU) 2. Create `messaging.js` in `app/assets/javascripts/` with this line: `//= require inboxes/faye` 3. Copy or replace 2 views from Inboxes example app to your application: [app/views/inboxes/messages/_form](https://github.com/kirs/inboxes-app/blob/master/app/views/inboxes/messages/_form.html.haml) and [app/views/inboxes/messages/create](https://github.com/kirs/inboxes-app/blob/master/app/views/inboxes/messages/create.js.erb) - + 4. Add config parameters to your application config (last 2 are not necessary): - + ```ruby config.inboxes.faye_enabled = true config.inboxes.faye_host = "inboxes-app.dev" # localhost by default @@ -67,4 +67,4 @@ You can read more about Faye on it's [official page](http://faye.jcoglan.com/). - [Alexander Brodyanoj](https://github.com/dom1nga) - [Dmitriy Kiriyenko](https://github.com/dmitriy-kiriyenko) -##Feel free for pull requests!
\ No newline at end of file +##Feel free for pull requests! diff --git a/app/controllers/inboxes/base_controller.rb b/app/controllers/inboxes/base_controller.rb index 680e38c..f1e77b1 100644 --- a/app/controllers/inboxes/base_controller.rb +++ b/app/controllers/inboxes/base_controller.rb @@ -1,10 +1,10 @@ class Inboxes::BaseController < ApplicationController private - + def init_discussion @discussion = Discussion.find(params[:discussion_id]) end - + # Needs to be overriden so that we use Spree's Ability rather than anyone else's. def current_ability # raise "Loading Ability" diff --git a/app/controllers/inboxes/discussions_controller.rb b/app/controllers/inboxes/discussions_controller.rb index c03a062..04a8edd 100644 --- a/app/controllers/inboxes/discussions_controller.rb +++ b/app/controllers/inboxes/discussions_controller.rb @@ -3,7 +3,7 @@ class Inboxes::DiscussionsController < Inboxes::BaseController # before_filter :authenticate_user! # before_filter :init_and_check_permissions, :only => :show before_filter :load_and_check_discussion_recipient, :only => [:create, :new] - + def index @discussions = current_user.discussions end @@ -21,37 +21,37 @@ class Inboxes::DiscussionsController < Inboxes::BaseController # @discussion = Discussion.new @discussion.messages.build end - + # POST /discussions # POST /discussions.json def create # @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 - + if @discussion.save redirect_to @discussion, :notice => t("inboxes.discussions.started") else render :action => "new" end end - + private - + # def init_and_check_permissions # @discussion = Discussion.includes(:messages, :speakers).find(params[:id]) # redirect_to discussions_url, :notice => t("inboxes.discussions.can_not_participate") unless @discussion.can_participate?(current_user) # end - + def load_and_check_discussion_recipient # initializing model for new and create actions @discussion = Discussion.new(params[:discussion].presence || {}) # @discussion.recipient_tokens = params[:recipients] if params[:recipients] # pre-population - + # checking if discussion with this user already exists if @discussion.recipient_ids && @discussion.recipient_ids.size == 1 user = User.find(@discussion.recipient_ids.first) diff --git a/app/controllers/inboxes/messages_controller.rb b/app/controllers/inboxes/messages_controller.rb index 4089b13..697592a 100644 --- a/app/controllers/inboxes/messages_controller.rb +++ b/app/controllers/inboxes/messages_controller.rb @@ -3,12 +3,12 @@ class Inboxes::MessagesController < Inboxes::BaseController # load_and_authorize_resource load_and_authorize_resource :discussion load_resource :message, :through => :discussion, :shallow => true - + def create @message.user = current_user @message.discussion = @discussion @message.save - + respond_to do |format| format.html { redirect_to @message.discussion } format.js @@ -16,7 +16,7 @@ class Inboxes::MessagesController < Inboxes::BaseController end # private - # + # # def init_and_check_permissions # @discussion = Discussion.find(params[:discussion_id]) # redirect_to discussions_url, :notice => t("inboxes.discussions.can_not_participate") unless @discussion.can_participate?(current_user) diff --git a/app/controllers/inboxes/speakers_controller.rb b/app/controllers/inboxes/speakers_controller.rb index 96a3049..30c0b7f 100644 --- a/app/controllers/inboxes/speakers_controller.rb +++ b/app/controllers/inboxes/speakers_controller.rb @@ -3,14 +3,14 @@ class Inboxes::SpeakersController < Inboxes::BaseController load_and_authorize_resource :discussion load_resource :speaker, :through => :discussion, :shallow => true # load_and_authorize_resource - + def create raise ActiveRecord::RecordNotFound unless params[:speaker] && params[:speaker][:user_id] @user = User.find(params[:speaker][:user_id]) flash[:notice] = t("inboxes.speakers.added") if @discussion.add_speaker(@user) redirect_to @discussion end - + def destroy @speaker = Speaker.find(params[:id]) @speaker.destroy diff --git a/app/models/discussion.rb b/app/models/discussion.rb index ca56596..7230538 100644 --- a/app/models/discussion.rb +++ b/app/models/discussion.rb @@ -84,20 +84,26 @@ class Discussion < ActiveRecord::Base end end + # return amount of unreaded messages for current discussion + def count_unread_mess(user) + speaker = find_speaker_by_user(user) + messages.where("updated_at > ?", speaker.updated_at ).where("user_id != ?", speaker.id ).count + end + def mark_as_read_for(user) speaker = Speaker.find_or_create_by_user_id_and_discussion_id(user.id, self.id) # flag.update_attributes(:updat => Time.zone.now) speaker.touch end - + def find_speaker_by_user user Speaker.find_by_discussion_id_and_user_id(self.id, user.id) end - + private def check_that_has_at_least_two_users errors.add :recipient_tokens, t("inboxes.discussions.choose_at_least_one_recipient") if !self.recipient_ids || self.recipient_ids.size < 2 end -end
\ No newline at end of file +end diff --git a/app/models/inboxes/ability.rb b/app/models/inboxes/ability.rb index 4d8408f..deb3d81 100644 --- a/app/models/inboxes/ability.rb +++ b/app/models/inboxes/ability.rb @@ -15,20 +15,20 @@ module Inboxes # and therefore should be easy to test in isolation. def self.register_ability(ability) self.abilities.add(ability) - + end def initialize(user) # raise "Initializing 3rd patry" # self.clear_aliased_actions - + # can [:index, :create], Discussion # can :read, Discussion do |discussion| # discussion.can_participate?(user) # end #include any abilities registered by extensions, etc. - + Ability.abilities.each do |clazz| ability = clazz.send(:new, user) @rules = rules + ability.send(:rules) diff --git a/app/models/message.rb b/app/models/message.rb index 3579e63..436775b 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,20 +1,20 @@ class Message < ActiveRecord::Base default_scope order(:created_at) - + belongs_to :discussion, :counter_cache => true, :touch => true belongs_to :user - + validates :user, :discussion, :body, :presence => true - + after_save :mark_discussion_as_read - + def visible_for? user self.created_at.to_i >= self.discussion.user_invited_at(user).to_i end - + private - + def mark_discussion_as_read self.discussion.mark_as_read_for(self.user) end diff --git a/app/models/speaker.rb b/app/models/speaker.rb index 0ed9cdd..4d8795b 100644 --- a/app/models/speaker.rb +++ b/app/models/speaker.rb @@ -1,16 +1,16 @@ class Speaker < ActiveRecord::Base belongs_to :user belongs_to :discussion - + validates_uniqueness_of :user_id, :scope => :discussion_id validates :user, :discussion, :presence => true - + after_destroy :destroy_discussion - + private - + def destroy_discussion self.discussion.destroy unless self.discussion.speakers.any? end - + end diff --git a/app/views/inboxes/discussions/_form.html.haml b/app/views/inboxes/discussions/_form.html.haml index 303f0bb..800004b 100644 --- a/app/views/inboxes/discussions/_form.html.haml +++ b/app/views/inboxes/discussions/_form.html.haml @@ -8,5 +8,5 @@ = j.label :body %br = j.text_area :body - + %p= f.submit
\ No newline at end of file diff --git a/app/views/inboxes/discussions/index.html.haml b/app/views/inboxes/discussions/index.html.haml index 86ae297..3d47a59 100644 --- a/app/views/inboxes/discussions/index.html.haml +++ b/app/views/inboxes/discussions/index.html.haml @@ -11,7 +11,7 @@ %td = link_to discussion.users.collect{|u| u[Inboxes::config.user_name]}.join(', '), discussion %td - = discussion.unread_for?(current_user) ? "Yes" : "No" - + = discussion.unread_for?(current_user) ? (discussion.count_unread_mess(current_user).to_s + ' сообщение') : "No" + %p - = link_to "Create new", new_discussion_path
\ No newline at end of file + = link_to "Create new", new_discussion_path diff --git a/app/views/inboxes/discussions/show.html.haml b/app/views/inboxes/discussions/show.html.haml index a910b51..50f5d19 100644 --- a/app/views/inboxes/discussions/show.html.haml +++ b/app/views/inboxes/discussions/show.html.haml @@ -16,13 +16,13 @@ = f.label :user_id, "Add speaker" = f.collection_select :user_id, available_users, :id, :name = f.submit "Add" - - + + %h3 Messages #messages_box = render @discussion.messages, :as => :message - + %h3 Add message = render "inboxes/messages/form" diff --git a/config/routes.rb b/config/routes.rb index 9da2d4a..3ecb989 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - + resources :discussions, :except => :edit, :module => :inboxes do resources :messages, :only => [:create, :index] resources :speakers, :only => [:create, :destroy] @@ -7,5 +7,5 @@ Rails.application.routes.draw do post 'leave' end end - + end
\ No newline at end of file diff --git a/lib/generators/inboxes/install_generator.rb b/lib/generators/inboxes/install_generator.rb index d5d2a97..a3169a2 100644 --- a/lib/generators/inboxes/install_generator.rb +++ b/lib/generators/inboxes/install_generator.rb @@ -5,9 +5,9 @@ module Inboxes module Generators class InstallGenerator < Rails::Generators::Base include Rails::Generators::Migration - + source_root File.expand_path("../templates", __FILE__) - + # desc "Generates migration for Discussion, Message, Speaker and DiscussionView models" def self.orm diff --git a/lib/generators/inboxes/templates/install.rb b/lib/generators/inboxes/templates/install.rb index 91fded7..436ac98 100644 --- a/lib/generators/inboxes/templates/install.rb +++ b/lib/generators/inboxes/templates/install.rb @@ -9,14 +9,14 @@ class InstallInboxes < ActiveRecord::Migration t.references :user t.references :discussion t.text :body - + t.timestamps end - + create_table :speakers do |t| t.references :user t.references :discussion - + t.timestamps end end diff --git a/lib/generators/inboxes/views_generator.rb b/lib/generators/inboxes/views_generator.rb index 117c05d..e9a8818 100644 --- a/lib/generators/inboxes/views_generator.rb +++ b/lib/generators/inboxes/views_generator.rb @@ -5,7 +5,7 @@ module Inboxes 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".' - + # TODO support of both haml and erb def copy_or_fetch filename_pattern = File.join self.class.source_root, "*" #/*.html.#{template_engine}" diff --git a/lib/inboxes.rb b/lib/inboxes.rb index f1b9c7a..57dc9a0 100644 --- a/lib/inboxes.rb +++ b/lib/inboxes.rb @@ -6,7 +6,7 @@ require "inboxes/active_record_extension" module Inboxes - + def self.configure(&block) yield @config ||= Inboxes::Configuration.new end @@ -25,11 +25,11 @@ module Inboxes config_accessor :faye_enabled def param_name - config.param_name.respond_to?(:call) ? config.param_name.call() : config.param_name + config.param_name.respond_to?(:call) ? config.param_name.call() : config.param_name end end - + # adding method inboxes for models ActiveRecord::Base.extend(Inboxes::ActiveRecordExtension) - + end diff --git a/lib/inboxes/ability.rb b/lib/inboxes/ability.rb index 3ed216b..c9f8d6b 100644 --- a/lib/inboxes/ability.rb +++ b/lib/inboxes/ability.rb @@ -13,17 +13,17 @@ module Inboxes discussion.can_participate?(user) end end - + # Message # can :create, Message do |message| # message.discussion.can_participate?(user) # end - # + # # # Speaker # can [:create, :destroy], Speaker do |speaker| # speaker.discussion.can_participate?(user) # end end end - + end
\ No newline at end of file diff --git a/lib/inboxes/active_record_extension.rb b/lib/inboxes/active_record_extension.rb index 34fd387..2a5bac8 100644 --- a/lib/inboxes/active_record_extension.rb +++ b/lib/inboxes/active_record_extension.rb @@ -3,7 +3,7 @@ module Inboxes def has_inboxes(options = {}) # field = options[:as] || name # prefix = options[:prefix] || "with" - + has_many :speakers, :dependent => :destroy has_many :discussions, :through => :speakers end diff --git a/lib/inboxes/engine.rb b/lib/inboxes/engine.rb index 01f63b5..b61eebc 100644 --- a/lib/inboxes/engine.rb +++ b/lib/inboxes/engine.rb @@ -5,7 +5,7 @@ module Inboxes def self.activate Ability.register_ability(InboxesAbility) end - + config.to_prepare &method(:activate).to_proc end end
\ No newline at end of file diff --git a/lib/inboxes/railtie.rb b/lib/inboxes/railtie.rb index 673784e..1626b20 100644 --- a/lib/inboxes/railtie.rb +++ b/lib/inboxes/railtie.rb @@ -4,7 +4,7 @@ require "inboxes/ability" module Inboxes class Railtie < ::Rails::Railtie config.inboxes = ActiveSupport::OrderedOptions.new - + initializer "inboxes.configure" do |app| Inboxes.configure do |config| config.user_name = app.config.inboxes[:user_name] || "email" @@ -15,7 +15,7 @@ module Inboxes # app.config.middleware.insert_before "::Rails::Rack::Logger", "Inboxes::Middleware" end - + # def self.activate # Ability.register_ability(InboxesAbility) # end |
