diff options
18 files changed, 91 insertions, 45 deletions
@@ -30,11 +30,28 @@ If you have problems with installation, you can check [code of demo app](https:/ By default, the gem provides localized phrases for Russian and English languages. You can easily override any of them. [Here is](https://github.com/kirs/inboxes/blob/master/config/locales/en.yml) list of all I18n phrases. +#Integration with Faye + +1. Add `gem "faye"` to your Gemfile and run `bundle install`. Install Faye by [the screencast](http://railscasts.com/episodes/260-messaging-with-faye) +2. Create `messaging.js` in `app/assets/javascripts/` with these lines: + + /* + = 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): + + config.inboxes.faye_enabled = true + config.inboxes.faye_host = "inboxes-app.dev" # localhost by default + config.inboxes.faye_port = 9292 # 9292 by default + +5. Faye installation is finished. If you have any troubles, check the [example app](https://github.com/kirs/inboxes-app/) + ##Todo -- Add rspec tests -- Move gem resources to namespace -- Describe integration with Faye +- Add RSpec tests ##Authors diff --git a/app/controllers/inboxes/base_controller.rb b/app/controllers/inboxes/base_controller.rb new file mode 100644 index 0000000..54767d2 --- /dev/null +++ b/app/controllers/inboxes/base_controller.rb @@ -0,0 +1,3 @@ +class Inboxes::BaseController < ApplicationController + +end
\ No newline at end of file diff --git a/app/controllers/discussions_controller.rb b/app/controllers/inboxes/discussions_controller.rb index 5e769fb..d901a80 100644 --- a/app/controllers/discussions_controller.rb +++ b/app/controllers/inboxes/discussions_controller.rb @@ -1,7 +1,6 @@ -class DiscussionsController < ApplicationController +class Inboxes::DiscussionsController < Inboxes::BaseController before_filter :authenticate_user! - # before_filter :check_permissions, :only => :show - + before_filter :init_and_check_permissions, :only => :show before_filter :load_and_check_discussion_recipient, :only => [:create, :new] def index @@ -11,10 +10,7 @@ class DiscussionsController < ApplicationController # GET /discussions/1 # GET /discussions/1.json def show - @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) - - # @discussion.mark_as_read_for(current_user) + @discussion.mark_as_read_for(current_user) end # GET /discussions/new @@ -44,6 +40,11 @@ class DiscussionsController < ApplicationController 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 fir new and create actions @discussion = Discussion.new((params[:discussion] ? params[:discussion] : {})) @@ -63,5 +64,4 @@ class DiscussionsController < ApplicationController end end end - -end +end
\ No newline at end of file diff --git a/app/controllers/messages_controller.rb b/app/controllers/inboxes/messages_controller.rb index 7baf8df..1c63221 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/inboxes/messages_controller.rb @@ -1,4 +1,4 @@ -class MessagesController < ApplicationController +class Inboxes::MessagesController < Inboxes::BaseController def create @discussion = Discussion.find(params[:discussion_id]) diff --git a/app/controllers/speakers_controller.rb b/app/controllers/inboxes/speakers_controller.rb index db90dbe..035df98 100644 --- a/app/controllers/speakers_controller.rb +++ b/app/controllers/inboxes/speakers_controller.rb @@ -1,4 +1,4 @@ -class SpeakersController < ApplicationController +class Inboxes::SpeakersController < Inboxes::BaseController before_filter :init_and_check_permissions def create diff --git a/app/helpers/inboxes_helper.rb b/app/helpers/inboxes_helper.rb new file mode 100644 index 0000000..87bb763 --- /dev/null +++ b/app/helpers/inboxes_helper.rb @@ -0,0 +1,9 @@ +require 'net/http' +module InboxesHelper + def inboxes_faye_broadcast(channel, &block) + message = {:channel => channel, :data => capture(&block), :ext => {:auth_token => defined?(FAYE_TOKEN) ? FAYE_TOKEN : ""}} + uri = URI.parse("http://#{Inboxes::config.faye_host}:#{Inboxes::config.faye_port}/faye") + # Rails.logger.info "Faye URL: #{uri}" + res = Net::HTTP.post_form(uri, :message => message.to_json) + end +end
\ No newline at end of file diff --git a/app/views/discussions/_form.html.haml b/app/views/inboxes/discussions/_form.html.haml index 303f0bb..303f0bb 100644 --- a/app/views/discussions/_form.html.haml +++ b/app/views/inboxes/discussions/_form.html.haml diff --git a/app/views/discussions/index.html.haml b/app/views/inboxes/discussions/index.html.haml index 8060254..7c4de6e 100644 --- a/app/views/discussions/index.html.haml +++ b/app/views/inboxes/discussions/index.html.haml @@ -1,5 +1,6 @@ %h1 Discussions list - +%p + Unread messages: %table %tr %th Last message diff --git a/app/views/discussions/new.html.haml b/app/views/inboxes/discussions/new.html.haml index 6bcf481..6bcf481 100644 --- a/app/views/discussions/new.html.haml +++ b/app/views/inboxes/discussions/new.html.haml diff --git a/app/views/discussions/show.html.haml b/app/views/inboxes/discussions/show.html.haml index e25d0a2..a910b51 100644 --- a/app/views/discussions/show.html.haml +++ b/app/views/inboxes/discussions/show.html.haml @@ -1,3 +1,6 @@ +- if Inboxes::config.faye_enabled + = javascript_include_tag "messaging" + %h1 Discussion %h3 @@ -17,22 +20,11 @@ %h3 Messages -%table - %tr - %th User - %th Message - %th Posted at - - @discussion.messages.each do |message| - %tr - %td - = message.user[Inboxes::config.user_name] - %td - = message.body - %td - = l(message.created_at) - +#messages_box + = render @discussion.messages, :as => :message + %h3 Add message -= render "messages/form" += render "inboxes/messages/form" %p = link_to "Leave discussion", discussion_speaker_path(@discussion, @discussion.find_speaker_by_user(current_user)), :method => :delete unless @discussion.private? diff --git a/app/views/messages/_form.html.haml b/app/views/inboxes/messages/_form.html.haml index f85ed37..f85ed37 100644 --- a/app/views/messages/_form.html.haml +++ b/app/views/inboxes/messages/_form.html.haml diff --git a/app/views/inboxes/messages/_message.html.haml b/app/views/inboxes/messages/_message.html.haml new file mode 100644 index 0000000..639f130 --- /dev/null +++ b/app/views/inboxes/messages/_message.html.haml @@ -0,0 +1,8 @@ +.message + %p + %span + = message.user[Inboxes::config.user_name] + @ + = l(message.created_at) + =":" + %b= message.body
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a9bce94..9da2d4a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do - resources :discussions, :except => :edit do + resources :discussions, :except => :edit, :module => :inboxes do resources :messages, :only => [:create, :index] resources :speakers, :only => [:create, :destroy] member do diff --git a/inboxes.gemspec b/inboxes.gemspec index ff3c296..be61064 100644 --- a/inboxes.gemspec +++ b/inboxes.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.version = Inboxes::VERSION s.authors = ["Kir Shatrov"] s.email = ["razor.psp@gmail.com"] - s.homepage = "" + s.homepage = "http://evrone.com/" s.summary = %q{Messaging system for Rails 3 app} s.description = %q{Messaging system for Rails 3 app} @@ -19,6 +19,15 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] # specify any dependencies here; for example: - s.add_development_dependency "ruby-debug" + # s.add_development_dependency "ruby-debug" s.add_runtime_dependency "haml-rails" + # s.add_runtime_dependency "inherited_resources" + + # s.add_development_dependency 'dm-sqlite-adapter', ['>= 1.1.0'] + s.add_development_dependency 'rspec', ['>= 0'] + s.add_development_dependency 'rspec-rails', ['>= 0'] + # s.add_development_dependency 'rr', ['>= 0'] + # s.add_development_dependency 'steak', ['>= 0'] + # s.add_development_dependency 'capybara', ['>= 0'] + s.add_development_dependency 'database_cleaner', ['>= 0'] end diff --git a/lib/inboxes.rb b/lib/inboxes.rb index 49bec39..133aa9c 100644 --- a/lib/inboxes.rb +++ b/lib/inboxes.rb @@ -18,6 +18,9 @@ module Inboxes class Configuration #:nodoc: include ActiveSupport::Configurable config_accessor :user_name + config_accessor :faye_host + config_accessor :faye_port + config_accessor :faye_enabled def param_name config.param_name.respond_to?(:call) ? config.param_name.call() : config.param_name diff --git a/lib/inboxes/railtie.rb b/lib/inboxes/railtie.rb index 06ab0d3..9e5435e 100644 --- a/lib/inboxes/railtie.rb +++ b/lib/inboxes/railtie.rb @@ -1,26 +1,18 @@ require 'rails' module Inboxes - class Railtie < ::Rails::Railtie #:nodoc: + 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" + config.faye_enabled = app.config.inboxes[:faye_enabled] || false + config.faye_host = app.config.inboxes[:faye_host] || "localhost" + config.faye_port = app.config.inboxes[:faye_port] || "9292" 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 diff --git a/vendor/assets/javascripts/inboxes/faye.js b/vendor/assets/javascripts/inboxes/faye.js new file mode 100644 index 0000000..cfc9cf3 --- /dev/null +++ b/vendor/assets/javascripts/inboxes/faye.js @@ -0,0 +1,3 @@ +/* += require_directory ./faye +*/
\ No newline at end of file diff --git a/vendor/assets/javascripts/inboxes/faye/init.js.coffee b/vendor/assets/javascripts/inboxes/faye/init.js.coffee new file mode 100644 index 0000000..425d74a --- /dev/null +++ b/vendor/assets/javascripts/inboxes/faye/init.js.coffee @@ -0,0 +1,9 @@ +$ -> + fayeUrl = window.location.origin + ":9292/faye" # change port for post of your Faye daemon + fayeJS = fayeUrl + ".js" + $.getScript(fayeJS, (e)-> + faye = new Faye.Client(fayeUrl) + faye.subscribe(window.location.pathname, (data)-> + eval(data) + ) + )
\ No newline at end of file |
