humanity 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ module Humanity
2
+ class Assignment < ActiveRecord::Base
3
+ belongs_to :human, polymorphic: true
4
+ belongs_to :role
5
+
6
+ attr_accessible # none
7
+
8
+ scope :by_source, ->(source) { where(source: source) }
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Humanity
2
+ class Role < ActiveRecord::Base
3
+ has_many :assignments, dependent: :destroy
4
+
5
+ attr_accessible # none
6
+
7
+ validates_presence_of :name
8
+
9
+ def to_s
10
+ name
11
+ end
12
+
13
+ def to_sym
14
+ to_s.to_sym
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class HumanityGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.join(File.dirname(__FILE__), 'templates')
7
+
8
+ def self.next_migration_number(dirname)
9
+ sleep 1
10
+ if ActiveRecord::Base.timestamped_migrations
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ else
13
+ "%.3d" % (current_migration_number(dirname) + 1)
14
+ end
15
+ end
16
+
17
+ def create_migration_file
18
+ migration_template 'create_users.rb', 'db/migrate/create_users.rb'
19
+ migration_template 'create_roles.rb', 'db/migrate/create_roles.rb'
20
+ migration_template 'create_assignments.rb', 'db/migrate/create_assignments.rb'
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class CreateAssignments < ActiveRecord::Migration
2
+ def change
3
+ create_table :assignments do |t|
4
+ t.references :human, polymorphic: true
5
+ t.references :role, null: false
6
+ t.string :source
7
+ end
8
+
9
+ add_index :assignments, :human_id
10
+ add_index :assignments, :role_id
11
+ add_index :assignments, :source
12
+ add_index :assignments, [:human_id, :role_id]
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table :roles do |t|
4
+ t.string :name, null: false
5
+ end
6
+
7
+ add_index :roles, :name
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :username
5
+ t.string :first_name
6
+ t.string :last_name
7
+ t.string :title
8
+ t.string :email
9
+ t.string :department
10
+ t.string :photo_url
11
+ t.datetime :current_login_at
12
+ t.datetime :last_login_at
13
+ t.integer :login_count
14
+ t.timestamps
15
+ end
16
+
17
+ add_index :users, :username
18
+ add_index :users, :email
19
+ add_index :users, :last_name
20
+ add_index :users, :first_name
21
+ add_index :users, [:last_name, :first_name]
22
+ add_index :users, [:first_name, :last_name]
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ module Humanity
2
+ module Base
3
+ def self.included(base)
4
+ base.has_many :assignments, as: :human, dependent: :destroy, class_name: 'Humanity::Assignment'
5
+ base.has_many :roles, through: :assignments, class_name: 'Humanity::Role'
6
+
7
+ base.validates_presence_of :username
8
+ base.validates_uniqueness_of :username
9
+ end
10
+
11
+ def name
12
+ "#{first_name} #{last_name}".strip
13
+ end
14
+
15
+ def role_symbols
16
+ roles.map(&:to_sym)
17
+ end
18
+
19
+ def has_role?(role)
20
+ roles.where(name: role.to_s).first.present?
21
+ end
22
+
23
+ def admin?
24
+ has_role? :admin
25
+ end
26
+
27
+ def developer?
28
+ has_role? :developer
29
+ end
30
+
31
+ def update_roles!(role_names, source)
32
+ prev_roles = self.roles
33
+ new_roles = role_names.map { |n| Role.find_or_create_by_name(n) }
34
+
35
+ # Create new roles and/or update source
36
+ new_roles.each do |role|
37
+ assignment = Assignment.find_or_initialize_by_human_id_and_human_type_and_role_id(self.id, self.class.to_s, role.id)
38
+ assignment.source = source
39
+ assignments << assignment if assignment.changed?
40
+ end
41
+
42
+ # Remove roles we had but don't anymore
43
+ old_roles = (prev_roles - new_roles)
44
+ assignments.each do |a|
45
+ a.destroy if old_roles.include?(a.role) && a.source == source
46
+ end
47
+
48
+ roles.reload
49
+ end
50
+
51
+ def update_login_info!
52
+ self.last_login_at = current_login_at
53
+ self.current_login_at = Time.now
54
+ self.login_count = login_count.to_i + 1
55
+ self.save
56
+ end
57
+
58
+ def to_s
59
+ name
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ require 'humanity'
2
+ require 'rails'
3
+
4
+ module Humanity
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Humanity
2
+ VERSION = '0.1.0'
3
+ end
data/lib/humanity.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Humanity
2
+ require 'humanity/engine'
3
+
4
+ autoload :Base, 'humanity/base'
5
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: humanity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Crownoble
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: Mix in Humanity and get common user model functionality
31
+ email: adam.crownoble@biola.edu
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - app/models/humanity/assignment.rb
37
+ - app/models/humanity/role.rb
38
+ - lib/humanity/version.rb
39
+ - lib/humanity/engine.rb
40
+ - lib/humanity/base.rb
41
+ - lib/generators/humanity/humanity_generator.rb
42
+ - lib/generators/humanity/templates/create_roles.rb
43
+ - lib/generators/humanity/templates/create_assignments.rb
44
+ - lib/generators/humanity/templates/create_users.rb
45
+ - lib/humanity.rb
46
+ homepage: https://github.com/biola/humanity
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Common user functionality
70
+ test_files: []