pbw 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWRmZjM2Yzk2NmY5ZmRjYTM4MTY1NzFhM2U5MzJmN2IxMjIzMTg2ZA==
4
+ MDI1NDNjNWI0ZTkzMjgzOTZiZjA3MTFhNjFhNzVhMmMxMDhjMjRiNw==
5
5
  data.tar.gz: !binary |-
6
- NjU0Njg3MGZiMjBhMmViZjhlNGY5MzgxZDE4N2VmNGIwNjZlM2I2Yw==
6
+ NmRlZWQ1MmIxOGVlZjE3NGVlNWNiMTQwZDUwMjNjNDFiY2NlYzg0ZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NGE0NWRlYTliZDA4YzExZDI2YmViMjIwMDcxNTY2NjRlNDUyZWRlZjliY2Jk
10
- ZjViYmRjZWNlYWNjNWExNzA1Mzc3ZWVmNGQ2NjNjMTM5OWRlZTQ3NWI0MjYx
11
- ZGRiZmE3ZjRmYzUxMWZkYWRlNzA2ODBlZWYyNzJjYzg5ZDdjMDI=
9
+ OGJkNGM5ZDUxZWQ0YTZkY2I4MWRlYjQyYTdiM2U5Y2YwYTM2MDUwMGU0NzBk
10
+ MmE0OTFkNmFhYzA1YmZiNDg0NTNlZDY4ZjZlYzA0MjljMzMwNmYzMWFiNDQz
11
+ NzlhOWI1NTM3N2FjMDFlMGI4NGZhYjY4OWU0MTJhYWU5MGMzNDE=
12
12
  data.tar.gz: !binary |-
13
- YjVjZWFlZTBlYzBhZTE5YzM4YzhjNzg4NTcyMjNhMzgwOTQ3NzFiMDYwNTZm
14
- YzBiMWM2YmU3NjA0M2FjZWQ2NTVlODA4NDE5NWFmYjI4ZDY1NWUxOGRlMGI1
15
- OTdkZDgxMDIwYTBiZDJjNjIwMzQ2NzIyMmRlMzU4MTlkNDFlYWM=
13
+ ZjZlNmNmMjlmN2UyNTRkM2M1MTU1MzY4MGZkZGJhYzE3NmI2OTk1MDk2ODE5
14
+ NTNiYzhmOTk1OWNjYTliZGQ1MjQxMjZhZmM1M2MzZmJlNGMxNWMwMDA0ZDky
15
+ MjJkNzkxOTU4YTQ2YTczY2EyYTgxMWQyYzExMTc0NmFmZjRiMTA=
data/Rakefile CHANGED
@@ -19,7 +19,9 @@ namespace :backbone do
19
19
  task :download_latest do
20
20
  files = {
21
21
  'underscore.js'=>'http://underscorejs.org/underscore.js',
22
- 'backbone.js' => 'http://backbonejs.org/backbone.js'
22
+ 'backbone.js' => 'http://backbonejs.org/backbone.js',
23
+ 'backbone_datalink.js' => 'https://raw.github.com/codebrew/backbone-rails/master/vendor/assets/javascripts/backbone_datalink.js',
24
+ 'backbone_rails_sync.js' => 'https://raw.github.com/codebrew/backbone-rails/master/vendor/assets/javascripts/backbone_rails_sync.js'
23
25
  }
24
26
 
25
27
  vendor_dir = "vendor/assets/javascripts"
@@ -1,4 +1,26 @@
1
1
  module Pbw
2
2
  class ApplicationController < ActionController::Base
3
+
4
+ rescue_from ::CanCan::AccessDenied do |exception|
5
+ flash[:alert] = "Access denied. You are not authorized to access the requested page."
6
+ respond_to do |format|
7
+ format.json {render json: flash[:alert], status: 401}
8
+ format.html {redirect_to root_path}
9
+ end
10
+ end
11
+
12
+ protected
13
+
14
+ def self.permission
15
+ return name = self.name.gsub('Controller','').singularize.split('::').last.constantize.name rescue nil
16
+ end
17
+
18
+ def current_ability
19
+ @current_ability ||= Ability.new(current_user)
20
+ end
21
+
22
+ def load_permissions
23
+ @current_permissions = current_user.role.permissions.collect{|i| [i.subject_class, i.action]}
24
+ end
3
25
  end
4
26
  end
@@ -0,0 +1,43 @@
1
+ module Pbw
2
+ class RolesController < ApplicationController
3
+ respond_to :json
4
+
5
+ before_filter :authenticate_user!
6
+ before_filter :is_super_admin?
7
+
8
+ def index
9
+ @roles = Role.all.keep_if{|i| i.name != "Super Admin"}
10
+ render json: @roles
11
+ end
12
+
13
+ def show
14
+ @role = Role.find(params[:id])
15
+ @permissions = @role.permissions
16
+ render json: {role: @role, permissions: @permissions}
17
+ end
18
+
19
+ def edit
20
+ @role = Role.find(params[:id])
21
+ @permissions = Permission.all.keep_if{|i| ["Part"].include? i.subject_class}.compact
22
+ @role_permissions = @role.permissions.collect{|p| p.id}
23
+ render json: {role: @role, permissions: @permissions, role_permissions: @role_permissions}
24
+ end
25
+
26
+ def update
27
+ @role = Role.find(params[:id])
28
+ @role.permissions = []
29
+ @role.set_permissions(params[:permissions]) if params[:permissions]
30
+ if @role.save
31
+ render json: {notice: "Role updated"}
32
+ return
33
+ end
34
+ render 'edit'
35
+ end
36
+
37
+ private
38
+
39
+ def is_super_admin?
40
+ redirect_to root_path and return unless current_user.super_admin?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ class Ability
2
+ include ::CanCan::Ability
3
+
4
+ def initialize(user)
5
+ user.role.permissions.each do |permission|
6
+ if permission.subject_class == "all"
7
+ can permission.action.to_sym, permission.subject_class.to_sym
8
+ else
9
+ can permission.action.to_sym, permission.subject_class.constantize
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Pbw
2
+ class Permission
3
+ include ::Mongoid::Document
4
+ field :subject_class, type: String
5
+ field :action, type: String
6
+ field :name, type: String
7
+
8
+ attr_accessible :subject_class, :action, :name
9
+
10
+ has_and_belongs_to_many :roles
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ module Pbw
2
+ class Role
3
+ include ::Mongoid::Document
4
+ field :name, type: String
5
+ attr_accessible :name
6
+ has_many :users
7
+
8
+ validates :name, presence: true, uniqueness: true
9
+
10
+ has_and_belongs_to_many :permissions
11
+
12
+ def set_permission(subject_class, action)
13
+ p = Permission.where(subject_class: subject_class, action: action)
14
+ p = Permission.create(subject_class: subject_class, action: action) unless p
15
+ self.permissions << p
16
+ end
17
+
18
+ def set_permissions(permissions)
19
+ permissions.each do |id|
20
+ permission = Permission.find(id)
21
+ self.permissions << p
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ module Pbw
2
+ class User
3
+ include ::Mongoid::Document
4
+ # Include default devise modules. Others available are:
5
+ # :token_authenticatable, :confirmable,
6
+ # :lockable, :timeoutable and :omniauthable
7
+ devise :database_authenticatable, :registerable,
8
+ :recoverable, :rememberable, :trackable, :validatable
9
+
10
+ ## Database authenticatable
11
+ field :email, :type => String, :default => ""
12
+ field :encrypted_password, :type => String, :default => ""
13
+
14
+ ## Recoverable
15
+ field :reset_password_token, :type => String
16
+ field :reset_password_sent_at, :type => Time
17
+
18
+ ## Rememberable
19
+ field :remember_created_at, :type => Time
20
+
21
+ ## Trackable
22
+ field :sign_in_count, :type => Integer, :default => 0
23
+ field :current_sign_in_at, :type => Time
24
+ field :last_sign_in_at, :type => Time
25
+ field :current_sign_in_ip, :type => String
26
+ field :last_sign_in_ip, :type => String
27
+
28
+ field :name, :type => String
29
+ validates_presence_of :name
30
+ validates_uniqueness_of :name, :email, :case_sensitive => false
31
+ validates_format_of :email, :with => /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
32
+ validates_confirmation_of :password
33
+
34
+ belongs_to :role
35
+
36
+ ## Confirmable
37
+ field :confirmation_token, :type => String
38
+ field :confirmed_at, :type => Time
39
+ field :confirmation_sent_at, :type => Time
40
+ field :unconfirmed_email, :type => String # Only if using reconfirmable
41
+
42
+ ## Lockable
43
+ field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts
44
+ field :unlock_token, :type => String # Only if unlock strategy is :email or :both
45
+ field :locked_at, :type => Time
46
+
47
+ ## Token authenticatable
48
+ field :authentication_token, :type => String
49
+
50
+ def super_admin?
51
+ self.role.name == "Super Admin"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ require 'devise/orm/mongoid'
2
+
3
+ Devise.setup do |config|
4
+ config.router_name = :perens_instant_user
5
+ end
data/config/routes.rb CHANGED
@@ -1,2 +1,6 @@
1
1
  Pbw::Engine.routes.draw do
2
+ devise_for :users, {
3
+ class_name: 'Pbw::User',
4
+ module: :devise
5
+ }
2
6
  end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ This setups directories, configuration and essential files for PBW
3
+
4
+ Example:
5
+ rails generate pbw:install
@@ -0,0 +1,38 @@
1
+ require 'generators/pbw/resource_helpers'
2
+
3
+ module Pbw
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Pbw::Generators::ResourceHelpers
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
10
+ :desc => "Skip Git ignores and keeps"
11
+
12
+ def inject_backbone
13
+ inject_into_file "app/assets/javascripts/application.js", :before => "//= require_tree" do
14
+ "//= require underscore\n//= require backbone\n//= require backbone_rails_sync\n//= require backbone_datalink\n//= require #{application_name.underscore}\n"
15
+ end
16
+ end
17
+
18
+ def create_dir_layout
19
+ %W{routers models views templates}.each do |dir|
20
+ empty_directory "app/assets/javascripts/#{dir}"
21
+ create_file "app/assets/javascripts/#{dir}/.gitkeep" unless options[:skip_git]
22
+ end
23
+ end
24
+
25
+ def create_app_file
26
+ template "app.coffee", "app/assets/javascripts/#{application_name.underscore}.js.coffee"
27
+ end
28
+
29
+ def config_mongoid
30
+ generate "mongoid:config"
31
+ end
32
+
33
+ def add_engine_routes
34
+ route "mount Pbw::Engine, :at => '/'"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ #= require_self
2
+ #= require_tree ./templates
3
+ #= require_tree ./models
4
+ #= require_tree ./views
5
+ #= require_tree ./routers
6
+
7
+ window.<%= js_app_name %> =
8
+ Models: {}
9
+ Collections: {}
10
+ Routers: {}
11
+ Views: {}
@@ -0,0 +1,55 @@
1
+ module Pbw
2
+ module Generators
3
+ module ResourceHelpers
4
+
5
+ def backbone_path
6
+ "app/assets/javascripts"
7
+ end
8
+
9
+ def model_namespace
10
+ [js_app_name, "Models", class_name].join(".")
11
+ end
12
+
13
+ def singular_model_name
14
+ uncapitalize singular_name.camelize
15
+ end
16
+
17
+ def plural_model_name
18
+ uncapitalize(plural_name.camelize)
19
+ end
20
+
21
+ def collection_namespace
22
+ [js_app_name, "Collections", plural_name.camelize].join(".")
23
+ end
24
+
25
+ def view_namespace
26
+ [js_app_name, "Views", plural_name.camelize].join(".")
27
+ end
28
+
29
+ def router_namespace
30
+ [js_app_name, "Routers", plural_name.camelize].join(".")
31
+ end
32
+
33
+ def jst(action)
34
+ "templates/#{plural_name}/#{action}"
35
+ end
36
+
37
+ def js_app_name
38
+ application_name.camelize
39
+ end
40
+
41
+ def application_name
42
+ if defined?(Rails) && Rails.application
43
+ Rails.application.class.name.split('::').first
44
+ else
45
+ "application"
46
+ end
47
+ end
48
+
49
+ def uncapitalize(str)
50
+ str[0, 1].downcase << str[1..-1]
51
+ end
52
+
53
+ end
54
+ end
55
+ end
data/lib/pbw/engine.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Pbw
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace Pbw
4
+ engine_name 'pbw'
5
+
4
6
  config.generators do |g|
5
7
  g.orm :mongoid
6
8
  g.template_engine :erb
@@ -9,6 +11,10 @@ module Pbw
9
11
  g.helper :false
10
12
  g.javascript_engine :coffee
11
13
  end
12
- config.mongoid.logger = Logger.new($stdout, :warn)
14
+
15
+ def self.config(&block)
16
+ yield Engine.config if block
17
+ Engine.config
18
+ end
13
19
  end
14
20
  end
data/lib/pbw/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pbw
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/pbw.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'mongoid'
2
+ require 'devise'
1
3
  require "pbw/engine"
2
4
 
3
5
  module Pbw