user_announcements 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # user_announcements
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/user_announcements.png)](http://badge.fury.io/rb/user_announcements)
4
+ [![Build Status](https://travis-ci.org/stevedowney/user_announcements.png)](https://travis-ci.org/stevedowney/user_announcements)
5
+ [![Coverage Status](https://coveralls.io/repos/stevedowney/user_announcements/badge.png?branch=master)](https://coveralls.io/r/stevedowney/user_announcements?branch=master)
4
6
  [![Code Climate](https://codeclimate.com/github/stevedowney/user_announcements.png)](https://codeclimate.com/github/stevedowney/user_announcements)
5
7
 
6
8
  Manage and display site-wide announcements on a per-user basis.
@@ -0,0 +1,23 @@
1
+ class HiddenAnnouncementsController < ApplicationController
2
+ before_filter :ensure_current_user
3
+
4
+ def index
5
+ @announcements = AnnouncementFinder.past_or_current
6
+ @hidden_announcement_ids = HiddenAnnouncement.hidden_announcement_ids_for(current_user.id)
7
+ end
8
+
9
+ def create
10
+ @announcement_id = params.fetch(:announcement_id)
11
+ HiddenAnnouncement.create_for(current_user.id, @announcement_id)
12
+ respond_to do |format|
13
+ format.html { redirect_to :back }
14
+ format.js
15
+ end
16
+ end
17
+
18
+ def destroy
19
+ HiddenAnnouncement.delete_all(user_id: current_user.id, announcement_id: params.fetch(:announcement_id))
20
+ redirect_to action: 'index'
21
+ end
22
+
23
+ end
@@ -2,7 +2,7 @@ module UserAnnouncementsHelper
2
2
 
3
3
  # Helper method for displaying messages for +current_user+.
4
4
  def user_announcements(options={})
5
- return if controller.controller_name == 'user_announcements'
5
+ return if controller.controller_name == 'hidden_announcements'
6
6
 
7
7
  announcements_rows = AnnouncementFinder.current_for_user(current_user)
8
8
  divs = announcements_rows.map do |announcement|
@@ -22,33 +22,44 @@ module UserAnnouncementsHelper
22
22
 
23
23
  def announcement_div_bootstrap(announcement)
24
24
  div_for announcement, class: 'alert', style: 'width:40em' do
25
- link_to(hide_user_announcement_path(announcement), remote: true) do
25
+ link_to(*hide_announcement_link_args(announcement)) do
26
26
  content_tag(:button, raw('&times;'), type: 'button', class: 'close')
27
27
  end +
28
28
  announcement.message.html_safe
29
29
  end
30
30
  end
31
31
 
32
+ def hide_announcement_link_args(announcement)
33
+ url = hidden_announcements_path(announcement_id: announcement)
34
+ options = {
35
+ method: :post,
36
+ remote: true,
37
+ id: "hide_#{dom_id(announcement)}"
38
+ }
39
+
40
+ [url, options]
41
+ end
42
+
32
43
  def announcement_div_non_bootstrap(announcement)
33
44
  div_for(announcement, class: 'non-bootstrap') do
34
45
  announcement.message.html_safe +
35
- link_to("hide announcement", hide_user_announcement_path(announcement), remote: true)
46
+ link_to("hide announcement", *hide_announcement_link_args(announcement))
36
47
  end
37
48
  end
38
49
 
39
50
  def unhide_announcement_link(announcement)
40
51
  if @hidden_announcement_ids.include?(announcement.id)
41
52
  content_tag(:div) do
42
- link_to('Unhide', unhide_user_announcement_path(announcement), method: :post)
53
+ link_to('Unhide', hidden_announcement_path(announcement, announcement_id: announcement), method: :delete)
43
54
  end
44
55
  end
45
56
  end
46
57
 
47
58
  def ua_table_attrs
48
59
  if bootstrap?
49
- {class: "table table-striped table-bordered table-condensed table-hover"}
60
+ {class: "ua-table bootstrap table table-striped table-bordered table-condensed table-hover"}
50
61
  else
51
- {class: 'ua-table'}
62
+ {class: 'ua-table non-bootstrap'}
52
63
  end
53
64
  end
54
65
 
@@ -1,7 +1,7 @@
1
1
  class Announcement < ActiveRecord::Base
2
2
  attr_accessible :message, :starts_at, :ends_at, :active
3
3
 
4
- has_many :user_announcements, :dependent => :destroy
4
+ has_many :hidden_announcements, :dependent => :destroy
5
5
 
6
6
  validates_presence_of :message, :starts_at, :ends_at
7
7
 
@@ -35,9 +35,22 @@ class Announcement < ActiveRecord::Base
35
35
 
36
36
  def new_with_defaults
37
37
  new do |ann|
38
- ann.active = true
39
- ann.starts_at = Time.now.at_beginning_of_day
40
- ann.ends_at = 1.week.from_now.at_midnight
38
+ ann.active = get_default(:active)
39
+ ann.starts_at = get_default(:starts_at)
40
+ ann.ends_at = get_default(:ends_at)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def get_default(column)
47
+ config_key = "default_#{column}"
48
+ default = UserAnnouncements.config.send(config_key)
49
+
50
+ if default.is_a?(Proc)
51
+ default.call
52
+ else
53
+ default
41
54
  end
42
55
  end
43
56
 
@@ -14,10 +14,14 @@ class AnnouncementFinder
14
14
  end
15
15
 
16
16
  def past_or_current
17
- Announcement
17
+ Rails.logger.debug '***** start past_or_current'
18
+ r = Announcement
18
19
  .active
19
20
  .where("(starts_at is null or starts_at < :now)", :now => DateTime.now)
20
21
  .order('created_at desc')
22
+ Rails.logger.debug '***** end past_or_current'
23
+ # sleep 1
24
+ r
21
25
  end
22
26
 
23
27
  def current_for_user(user)
@@ -30,7 +34,7 @@ class AnnouncementFinder
30
34
  # end
31
35
 
32
36
  def for_user(user, type)
33
- hidden_announcement_ids = UserAnnouncement.hidden_announcement_ids_for(user.id)
37
+ hidden_announcement_ids = HiddenAnnouncement.hidden_announcement_ids_for(user.id)
34
38
  result = type
35
39
  result = result.where("id not in (?)", hidden_announcement_ids) if hidden_announcement_ids.present?
36
40
  result.sort_by(&:status_order)
@@ -0,0 +1,28 @@
1
+ class HiddenAnnouncement < ActiveRecord::Base
2
+ attr_accessible :user_id, :announcement_id
3
+
4
+ validates_presence_of :user_id, :announcement_id
5
+
6
+ class << self
7
+
8
+ def hidden_announcement_ids_for(user_id)
9
+ where(user_id: user_id).pluck(:announcement_id)
10
+ end
11
+
12
+ def create_for(user_id, announcement_id)
13
+ return if record_exists_for?(user_id, announcement_id)
14
+
15
+ create!(
16
+ user_id: user_id,
17
+ announcement_id: announcement_id
18
+ )
19
+ end
20
+
21
+ private
22
+
23
+ def record_exists_for?(user_id, announcement_id)
24
+ where(user_id: user_id, announcement_id: announcement_id).count > 0
25
+ end
26
+ end
27
+
28
+ end
@@ -1,7 +1,7 @@
1
1
  <tr id="<%= dom_id(announcement) %>" class='<%= announcement.status %>'>
2
2
  <td><%=raw announcement.message %></td>
3
- <td class='date'><%= announcement.starts_at.try(:to_s, :db) %></td>
4
- <td class='date'><%= announcement.ends_at.try(:to_s, :db) %></td>
3
+ <td class='date'><%= announcement.starts_at.to_s(:long) %></td>
4
+ <td class='date'><%= announcement.ends_at.to_s(:long) %></td>
5
5
  <td><%= announcement.status %></td>
6
6
  <td><%= boolean_display(announcement.active)%></td>
7
7
  <td><%= link_to('edit', edit_admin_announcement_path(announcement), :class => 'btn btn-mini') %></td>
@@ -0,0 +1 @@
1
+ $("#announcement_<%= j @announcement_id %>").remove();
@@ -8,7 +8,7 @@
8
8
  </tr>
9
9
  </thead>
10
10
  <tbody>
11
- <%= render partial: 'user_announcements/announcement', collection: @announcements %>
11
+ <%= render partial: 'hidden_announcements/announcement', collection: @announcements %>
12
12
  </tbody>
13
13
  <% end %>
14
14
 
data/config/routes.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
- resources :user_announcements, path: '/announcements' do
4
- get :hide, on: :member
5
- post :unhide, on: :member
6
- end
7
- # match 'announcements/:id/hide', to: 'announcements#hide', as: 'hide_announcement'
3
+ resources :hidden_announcements, path: '/announcements', only: [:index, :create, :destroy]
4
+
8
5
  namespace :admin do
9
6
  resources :announcements
10
7
  end
@@ -1,17 +1,21 @@
1
1
  table.ua-table {
2
- border: 1px solid black;
3
-
4
- th, td {
5
- border: 1px solid black;
6
- padding: 0.5em;
2
+
3
+ td.date {
4
+ white-space: nowrap;
7
5
  }
8
6
 
9
- td {
10
- vertical-align: top;
11
-
12
- &.date {
13
- white-space: nowrap;
7
+ &.non-bootstrap {
8
+ border: 1px solid black;
9
+
10
+ th, td {
11
+ border: 1px solid black;
12
+ padding: 0.5em;
13
+ }
14
+
15
+ td {
16
+ vertical-align: top;
14
17
  }
18
+
15
19
  }
16
20
  }
17
21
 
@@ -1,6 +1,9 @@
1
1
  UserAnnouncements.config do |c|
2
2
 
3
- # uncomment to user Bootstrap styling
4
- # c.bootstrap = true
3
+ # config.bootstrap = true
4
+
5
+ # config.default_active = true
6
+ # config.default_starts_at = lambda { Time.now.in_time_zone }
7
+ # config.default_ends_at = lambda { 1.week.from_now.in_time_zone.end_of_day }
5
8
 
6
9
  end
@@ -10,13 +10,13 @@ class CreateUserAnnouncementTables < ActiveRecord::Migration
10
10
  t.timestamps
11
11
  end
12
12
 
13
- create_table :user_announcements, :force => true do |t|
13
+ create_table :hidden_announcements, :force => true do |t|
14
14
  t.integer :user_id
15
15
  t.integer :announcement_id
16
16
  t.timestamps
17
17
  end
18
18
 
19
- add_index :user_announcements, :user_id
20
- add_index :user_announcements, :announcement_id
19
+ add_index :hidden_announcements, :user_id
20
+ add_index :hidden_announcements, :announcement_id
21
21
  end
22
22
  end
@@ -1,9 +1,19 @@
1
1
  module UserAnnouncements
2
+
2
3
  class Engine < ::Rails::Engine
3
4
  config.generators.integration_tool :rspec
4
5
  config.generators.test_framework :rspec
5
6
 
6
- config.bootstrap = false
7
+ config.bootstrap = true
8
+
9
+ config.default_active = true
10
+ config.default_starts_at = lambda { Time.now.in_time_zone }
11
+ config.default_ends_at = lambda { 1.week.from_now.in_time_zone.end_of_day }
12
+
13
+ config.roles = []
14
+ config.types = []
15
+ config.styles = %w(error succes info)
16
+
7
17
  end
8
18
 
9
19
  def self.config(&block)
@@ -1,3 +1,3 @@
1
1
  module UserAnnouncements
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: user_announcements
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-22 00:00:00.000000000 Z
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: capybara-webkit
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  - !ruby/object:Gem::Dependency
111
127
  name: guard-rspec
112
128
  requirement: !ruby/object:Gem::Requirement
@@ -139,6 +155,54 @@ dependencies:
139
155
  - - ! '>='
140
156
  - !ruby/object:Gem::Version
141
157
  version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: coveralls
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: shoulda-matchers
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: database_cleaner
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
142
206
  - !ruby/object:Gem::Dependency
143
207
  name: sqlite3
144
208
  requirement: !ruby/object:Gem::Requirement
@@ -156,7 +220,7 @@ dependencies:
156
220
  - !ruby/object:Gem::Version
157
221
  version: '0'
158
222
  - !ruby/object:Gem::Dependency
159
- name: bootstrap-view-helpers
223
+ name: launchy
160
224
  requirement: !ruby/object:Gem::Requirement
161
225
  none: false
162
226
  requirements:
@@ -276,19 +340,19 @@ extra_rdoc_files: []
276
340
  files:
277
341
  - app/controllers/admin/announcements_controller.rb
278
342
  - app/controllers/admin/base_controller.rb
279
- - app/controllers/user_announcements_controller.rb
343
+ - app/controllers/hidden_announcements_controller.rb
280
344
  - app/helpers/user_announcements_helper.rb
281
345
  - app/models/announcement.rb
282
346
  - app/models/announcement_finder.rb
283
- - app/models/user_announcement.rb
347
+ - app/models/hidden_announcement.rb
284
348
  - app/views/admin/announcements/_announcement.html.erb
285
349
  - app/views/admin/announcements/_form.html.erb
286
350
  - app/views/admin/announcements/edit.html.erb
287
351
  - app/views/admin/announcements/index.html.erb
288
352
  - app/views/admin/announcements/new.html.erb
289
- - app/views/user_announcements/_announcement.html.erb
290
- - app/views/user_announcements/hide.js.erb
291
- - app/views/user_announcements/index.html.erb
353
+ - app/views/hidden_announcements/_announcement.html.erb
354
+ - app/views/hidden_announcements/create.js.erb
355
+ - app/views/hidden_announcements/index.html.erb
292
356
  - config/routes.rb
293
357
  - lib/generators/user_announcements/install_generator.rb
294
358
  - lib/generators/user_announcements/templates/css.scss
@@ -1,26 +0,0 @@
1
- class UserAnnouncementsController < ApplicationController
2
- before_filter :ensure_current_user
3
-
4
- def index
5
- @announcements = AnnouncementFinder.past_or_current
6
- @hidden_announcement_ids = UserAnnouncement.hidden_announcement_ids_for(current_user.id)
7
- end
8
-
9
- def hide
10
- @announcement_id = params.fetch(:id)
11
- UserAnnouncement.create_for(current_user, @announcement_id)
12
- end
13
-
14
- def unhide
15
- UserAnnouncement.delete_all(user_id: current_user.id, announcement_id: params.fetch(:id))
16
- redirect_to action: 'index'
17
- end
18
-
19
- private
20
-
21
- def ensure_current_user
22
- rescue NameError
23
- raise "You must have a current_user method"
24
- end
25
-
26
- end
@@ -1,19 +0,0 @@
1
- class UserAnnouncement < ActiveRecord::Base
2
- attr_accessible :user_id, :announcement_id
3
-
4
- class << self
5
-
6
- def hidden_announcement_ids_for(user_id)
7
- where(user_id: user_id).pluck(:announcement_id)
8
- end
9
-
10
- def create_for(current_user, announcement_id)
11
- create!(
12
- user_id: current_user.id,
13
- announcement_id: announcement_id
14
- )
15
- end
16
-
17
- end
18
-
19
- end
@@ -1 +0,0 @@
1
- $("#announcement_<%= j params[:id] %>").remove();