gossiper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3cee602077f8193490a6c88820bfb282bc91e4d
4
+ data.tar.gz: f8880ec0eba28ba87c3b813632868918170c250b
5
+ SHA512:
6
+ metadata.gz: 597cc48ab2c7454143814fe67b1c83ac9b8d63ce0c8a99c2ff70cf468391e747c8cfbac106d42508c1cdae4d81493df2cc5f41250267503a65000c0fc8e0d32e
7
+ data.tar.gz: a5c247cb2bcc91f797fd1776088e98baf30a84065156e3ce5f7bed32cae3bec5c7356021927582f07b9a5bd3cd83cefa5bc8fa1887bc8a7e994a42db1b7c9922
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Marcelo Guilherme Jacobus Jr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Gossiper'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module Gossiper
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gossiper
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,60 @@
1
+ module Gossiper
2
+ class Notification < ActiveRecord::Base
3
+ STATUSES = %w(pending delivered)
4
+
5
+ validates :kind, presence: true
6
+
7
+ def user=(user)
8
+ @user = user
9
+ self.user_class = user.class.to_s
10
+ self.user_id = user.id
11
+ end
12
+
13
+ def user
14
+ @user ||= user_class.constantize.find(user_id)
15
+ end
16
+
17
+ def user_class
18
+ read_attribute(:user_class).presence || Gossiper.configuration.default_notification_user_class
19
+ end
20
+
21
+ def status
22
+ read_attribute(:status).presence || STATUSES.first
23
+ end
24
+
25
+ def deliver
26
+ mail.deliver
27
+ update_delivered_at!
28
+ end
29
+
30
+ def deliver!
31
+ mail.deliver!
32
+ update_delivered_at!
33
+ end
34
+
35
+ def kind=(value)
36
+ value = value.present? ? value.parameterize.underscore : nil
37
+ write_attribute(:kind, value)
38
+ end
39
+
40
+ def method_missing(method, *args, &block)
41
+ STATUSES.each do |status|
42
+ if method.to_s == "#{status}?"
43
+ return self.status == status
44
+ end
45
+ end
46
+ super(method, *args, &block)
47
+ end
48
+
49
+ protected
50
+ def mail
51
+ Gossiper::Mailer.mail_for(self)
52
+ end
53
+
54
+ def update_delivered_at!
55
+ self.delivered_at = Time.now
56
+ save!
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Gossiper</title>
5
+ <%= stylesheet_link_tag "gossiper/application", media: "all" %>
6
+ <%= javascript_include_tag "gossiper/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Gossiper::Engine.routes.draw do
2
+ end
@@ -0,0 +1,14 @@
1
+ class CreateGossiperNotifications < ActiveRecord::Migration
2
+ def change
3
+ create_table :gossiper_notifications do |t|
4
+ t.integer :user_id
5
+ t.string :user_class
6
+ t.string :kind
7
+ t.string :status
8
+ t.datetime :delivered_at
9
+ t.boolean :read
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Install the configuration files
3
+
4
+ Example:
5
+ rails generate gossiper:install
6
+
7
+ This will create:
8
+ config/initializers/gossiper.rb
9
+ config/locales/gossiper.en.yml
@@ -0,0 +1,11 @@
1
+ class Gossiper::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_initializer
5
+ template "initializer.rb", "config/initializer/gossiper.rb"
6
+ end
7
+
8
+ def copy_locale
9
+ template "locale.en.yml", "config/locale/gossiper.en.yml"
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ <%- test_folder = defined?(RSpec) ? 'rspec' : 'test' -%>
2
+ Gossiper.configure do |config|
3
+ # Change the default user class
4
+ configure.default_user_class = 'User'
5
+
6
+ # change the default class of the configurations
7
+ config.default_notification_base_class = 'Gossiper::EmailConfig'
8
+
9
+ # change the path where the notifications should be placed
10
+ config.notification_root_folder = Rails.root.join('app/models')
11
+
12
+ # the notifications test folder
13
+ config.notification_test_folder = Rails.root.join("<%= test_folder %>", "models")
14
+
15
+ config.authorize_with do |controller|
16
+ # Example:
17
+
18
+ # unless current_user.is_admin?
19
+ # redirect_to root_path, notice: 'Get out!'
20
+ # end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ gossiper:
3
+ notifications:
4
+ # Example
5
+ user_welcome:
6
+ subject: Welcome!
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a notification type configuration
3
+
4
+ Example:
5
+ rails generate notification_type user_welcome
6
+
7
+ This will create:
8
+ app/models/notifications/user_welcome_notification
@@ -0,0 +1,23 @@
1
+ class Gossiper::NotificationTypeGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_class_file
5
+ template 'notification_type.rb', File.join(
6
+ Gossiper.configuration.notifications_root_folder.to_s,
7
+ 'notifications',
8
+ class_path,
9
+ "#{singular_name}_notification.rb"
10
+ )
11
+ end
12
+
13
+ def create_test_file
14
+ test_sufix = defined?(RSpec) ? 'spec' : 'test'
15
+
16
+ template 'notification_type_test.rb', File.join(
17
+ Gossiper.configuration.notifications_test_folder.to_s,
18
+ 'notifications',
19
+ class_path,
20
+ "#{singular_name}_notification_#{test_sufix}.rb"
21
+ )
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Notifications
2
+ class <%= class_name %>Notification < Gossiper::EmailConfig
3
+
4
+ # def to
5
+ # end
6
+
7
+ # def bcc
8
+ # end
9
+
10
+ # def from
11
+ # end
12
+
13
+ # def cc
14
+ # end
15
+
16
+ # def template_name
17
+ # end
18
+
19
+ # def template_path
20
+ # end
21
+
22
+ # def subject
23
+ # end
24
+
25
+ # def attachments
26
+ # end
27
+
28
+ # def instance_variables
29
+ # end
30
+
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Notifications::<%= class_name %>Notification do
4
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ module Notifications
4
+ class <%= class_name %>NotificationTest < ActiveSupport::TestCase
5
+ end
6
+ end
@@ -0,0 +1,28 @@
1
+ module Gossiper
2
+ class Configuration
3
+
4
+ class << self
5
+ def attribute(name, default_value = nil)
6
+ define_method name do
7
+ instance_variable_get("@#{name}") || default_value
8
+ end
9
+
10
+ define_method "#{name}=" do |value|
11
+ instance_variable_set("@#{name}", value.to_s)
12
+ end
13
+ end
14
+ end
15
+
16
+ attribute :default_notification_user_class
17
+ attribute :default_notification_config_class
18
+ attribute :notifications_root_folder
19
+ attribute :notifications_test_folder
20
+
21
+ def authorize_with(&block)
22
+ if block_given?
23
+ @authorize_with = block
24
+ end
25
+ @authorize_with ||= Proc.new {}
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ module Gossiper
2
+ class EmailConfig
3
+ attr_reader :notification, :user
4
+
5
+ def initialize(notification)
6
+ @notification = notification
7
+ @user = notification.user
8
+ end
9
+
10
+ def to
11
+ if user.respond_to?(:name)
12
+ ["#{user.name} <#{user.email}>"]
13
+ else
14
+ [user.email]
15
+ end
16
+ end
17
+
18
+ # TODO: Make configuration for default bcc
19
+ def bcc
20
+ []
21
+ end
22
+
23
+ # TODO: Make configuration for default from
24
+ def from
25
+ end
26
+
27
+ # TODO: Make configuration for default cc
28
+ def cc
29
+ []
30
+ end
31
+
32
+ def template_name
33
+ "#{notification.kind}_notification"
34
+ end
35
+
36
+ def template_path
37
+ 'notifications'
38
+ end
39
+
40
+ def subject
41
+ I18n.t("gossiper.notifications.#{notification.kind}.subject")
42
+ end
43
+
44
+ def attachments
45
+ {}
46
+ end
47
+
48
+ def instance_variables
49
+ {}
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,10 @@
1
+ module Gossiper
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Gossiper
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.integration_tool :rspec
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ module Gossiper
2
+ class Mailer < ActionMailer::Base
3
+
4
+ def mail_for(notification)
5
+ config = config_for(notification)
6
+
7
+ config.attachments.each do |filename, file|
8
+ attachments[filename] = file
9
+ end
10
+
11
+ config.instance_variables.each do |name, value|
12
+ instance_variable_set("@#{name}", value)
13
+ end
14
+
15
+ mail(
16
+ to: config.to,
17
+ cc: config.cc,
18
+ bcc: config.bcc,
19
+ subject: config.subject,
20
+ template_name: config.template_name,
21
+ template_path: config.template_path
22
+ )
23
+ end
24
+
25
+ def config_for(notification)
26
+ begin
27
+ klass = "Notifications::#{notification.kind.classify}Notification"
28
+ klass.constantize.new(notification)
29
+ rescue NameError
30
+ Gossiper::EmailConfig.new(notification)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Gossiper
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gossiper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "gossiper/engine"
2
+ require "gossiper/email_config"
3
+ require "gossiper/mailer"
4
+ require "gossiper/configuration"
5
+
6
+ module Gossiper
7
+
8
+ class << self
9
+ def configuration
10
+ @@_config ||= Configuration.new
11
+ end
12
+
13
+ def config
14
+ yield configuration
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :gossiper do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gossiper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Jacobus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda-matchers
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec-rails
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: Eases the creation of email messages as well as user notification systems
90
+ email:
91
+ - marcelo.jacobus@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - app/assets/javascripts/gossiper/application.js
97
+ - app/assets/stylesheets/gossiper/application.css
98
+ - app/helpers/gossiper/application_helper.rb
99
+ - app/models/gossiper/notification.rb
100
+ - app/views/layouts/gossiper/application.html.erb
101
+ - app/controllers/gossiper/application_controller.rb
102
+ - config/routes.rb
103
+ - db/migrate/20131206162038_create_gossiper_notifications.rb
104
+ - lib/generators/gossiper/notification_type/USAGE
105
+ - lib/generators/gossiper/notification_type/notification_type_generator.rb
106
+ - lib/generators/gossiper/notification_type/templates/notification_type_spec.rb
107
+ - lib/generators/gossiper/notification_type/templates/notification_type_test.rb
108
+ - lib/generators/gossiper/notification_type/templates/notification_type.rb
109
+ - lib/generators/gossiper/install/USAGE
110
+ - lib/generators/gossiper/install/install_generator.rb
111
+ - lib/generators/gossiper/install/templates/initializer.rb
112
+ - lib/generators/gossiper/install/templates/locale.en.yml
113
+ - lib/gossiper.rb
114
+ - lib/tasks/gossiper_tasks.rake
115
+ - lib/gossiper/email_config.rb
116
+ - lib/gossiper/engine.rb
117
+ - lib/gossiper/mailer.rb
118
+ - lib/gossiper/configuration.rb
119
+ - lib/gossiper/version.rb
120
+ - MIT-LICENSE
121
+ - Rakefile
122
+ homepage: https://github.com/mjacobus/gossiper
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.0.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: It eases the creation of messages for the users
146
+ test_files: []