forked from rbriank/refinerycms_membership
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that loading happens automatically and support what it previou…
…sly was.
- Loading branch information
Showing
2 changed files
with
69 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1 @@ | ||
require 'refinery' | ||
|
||
module Refinery | ||
module Memberships | ||
class Engine < Rails::Engine | ||
initializer "static assets" do |app| | ||
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public" | ||
end | ||
|
||
config.after_initialize do | ||
Refinery::Plugin.register do |plugin| | ||
plugin.name = "memberships" | ||
plugin.activity = {:class => Membership} | ||
plugin.menu_match = /(refinery|admin)\/(memberships)?(page_roles)?(user_roles)?$/ | ||
end | ||
end # config.after_initialize | ||
|
||
config.to_prepare do | ||
Role.class_eval{ has_and_belongs_to_many :pages } | ||
|
||
Page.class_eval do | ||
has_and_belongs_to_many :roles | ||
|
||
def user_allowed?(user) | ||
# if a page has no roles assigned, let everyone see it | ||
if roles.blank? | ||
true | ||
|
||
else | ||
# if a page has roles, but the user doesn't or is nil | ||
if user.nil? || user.roles.blank? | ||
false | ||
|
||
# otherwise, check user vs. page roles | ||
else | ||
(roles & user.roles).any? | ||
|
||
end | ||
end | ||
|
||
# roles.blank? ? true : user.nil? ? false : roles.blank? || (roles & user.roles).any? | ||
end | ||
end | ||
|
||
PagesController.class_eval do | ||
def show | ||
# Find the page by the newer 'path' or fallback to the page's id if no path. | ||
@page = Page.find(params[:path] ? params[:path].to_s.split('/').last : params[:id]) | ||
|
||
if @page.user_allowed?(current_user) && | ||
(@page.try(:live?) || | ||
(refinery_user? and current_user.authorized_plugins.include?("refinery_pages"))) | ||
|
||
# if the admin wants this to be a "placeholder" page which goes to its first child, go to that instead. | ||
if @page.skip_to_first_child and (first_live_child = @page.children.order('lft ASC').where(:draft=>false).first).present? | ||
redirect_to first_live_child.url | ||
end | ||
else | ||
error_404 | ||
end | ||
|
||
end | ||
end # PagesController.class_eval | ||
end # config.to_prepare | ||
|
||
end # Engine < Rails::Engine | ||
end # Memberships | ||
end # Refinery | ||
require File.expand_path('../refinerycms-memberships', __FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'refinery' | ||
|
||
module Refinery | ||
module Memberships | ||
class Engine < Rails::Engine | ||
initializer "static assets" do |app| | ||
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public" | ||
end | ||
|
||
config.after_initialize do | ||
Refinery::Plugin.register do |plugin| | ||
plugin.name = "memberships" | ||
plugin.activity = {:class => Membership} | ||
plugin.menu_match = /(refinery|admin)\/(memberships)?(page_roles)?(user_roles)?$/ | ||
end | ||
end # config.after_initialize | ||
|
||
config.to_prepare do | ||
Role.class_eval{ has_and_belongs_to_many :pages } | ||
|
||
Page.class_eval do | ||
has_and_belongs_to_many :roles | ||
|
||
def user_allowed?(user) | ||
# if a page has no roles assigned, let everyone see it | ||
if roles.blank? | ||
true | ||
|
||
else | ||
# if a page has roles, but the user doesn't or is nil | ||
if user.nil? || user.roles.blank? | ||
false | ||
|
||
# otherwise, check user vs. page roles | ||
else | ||
(roles & user.roles).any? | ||
|
||
end | ||
end | ||
|
||
# roles.blank? ? true : user.nil? ? false : roles.blank? || (roles & user.roles).any? | ||
end | ||
end | ||
|
||
PagesController.class_eval do | ||
def show | ||
# Find the page by the newer 'path' or fallback to the page's id if no path. | ||
@page = Page.find(params[:path] ? params[:path].to_s.split('/').last : params[:id]) | ||
|
||
if @page.user_allowed?(current_user) && | ||
(@page.try(:live?) || | ||
(refinery_user? and current_user.authorized_plugins.include?("refinery_pages"))) | ||
|
||
# if the admin wants this to be a "placeholder" page which goes to its first child, go to that instead. | ||
if @page.skip_to_first_child and (first_live_child = @page.children.order('lft ASC').where(:draft=>false).first).present? | ||
redirect_to first_live_child.url | ||
end | ||
else | ||
error_404 | ||
end | ||
|
||
end | ||
end # PagesController.class_eval | ||
end # config.to_prepare | ||
|
||
end # Engine < Rails::Engine | ||
end # Memberships | ||
end # Refinery |