Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default notification template for development #25

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/helpers/notifications_helper.rb
@@ -0,0 +1,49 @@
module NotificationsHelper
end

class NotificationRenderer
attr_reader :view_context, :notification

def initialize(view_context, notification)
@view_context = view_context
@notification = notification
end

def render_or_stub
if Rails.env.development? && template_missing?
generate_template
end

view_context.render "notifications/#{notification.notifiable_type.underscore}/#{notification.action.parameterize.underscore}",
actor: notification.actor,
action: notification.action,
notifiable: notification.notifiable
end

private

def template_missing?
!view_context.lookup_context.exists?(
"#{notification.action}",
["notifications/#{notification.notifiable_type.underscore}"],
true
)
end

def generate_template
dir_name = "app/views/notifications/#{notification.notifiable_type.underscore}"
FileUtils.mkdir_p(dir_name)

full_path = "#{dir_name}/_#{notification.action.parameterize.underscore}.html.erb"
template = File.new(full_path, "wb")
template.puts default_notification
template.close
end

def default_notification
<<~'NOTICE'
<%# Available variables: actor, action, notifiable %>
<%= "#{actor.name} #{action} #{notifiable}" %>
NOTICE
end
end
4 changes: 3 additions & 1 deletion app/views/notifications/index.html.erb
Expand Up @@ -2,6 +2,8 @@

<ul>
<% @notifications.each do |notification| %>
<li><%= render "notifications/#{notification.notifiable_type.underscore}/#{notification.action.underscore}", actor: notification.actor, notifiable: notification.notifiable %></li>
<li>
<%= NotificationRenderer.new(self, notification).render_or_stub %>
</li>
<% end %>
</ul>