core_models 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/lib/core_models.rb +4 -0
  5. data/lib/core_models/models/concerns/group_concerns.rb +31 -0
  6. data/lib/core_models/models/concerns/role_concerns.rb +31 -0
  7. data/lib/core_models/models/concerns/user_concerns.rb +102 -0
  8. data/lib/core_models/models/group.rb +25 -0
  9. data/lib/core_models/models/group_membership.rb +16 -0
  10. data/lib/core_models/models/groups_role.rb +15 -0
  11. data/lib/core_models/models/permission.rb +24 -0
  12. data/lib/core_models/models/role.rb +33 -0
  13. data/lib/core_models/models/roles_permission.rb +15 -0
  14. data/lib/core_models/models/user.rb +63 -0
  15. data/lib/core_models/version.rb +3 -0
  16. data/lib/generators/core_models_setup_generator.rb +19 -0
  17. data/lib/tasks/core_models_tasks.rake +4 -0
  18. data/spec/dummy/README.rdoc +28 -0
  19. data/spec/dummy/Rakefile +6 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  21. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  22. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/spec/dummy/bin/bundle +3 -0
  26. data/spec/dummy/bin/rails +4 -0
  27. data/spec/dummy/bin/rake +4 -0
  28. data/spec/dummy/bin/setup +29 -0
  29. data/spec/dummy/config.ru +4 -0
  30. data/spec/dummy/config/application.rb +26 -0
  31. data/spec/dummy/config/boot.rb +5 -0
  32. data/spec/dummy/config/database.yml +23 -0
  33. data/spec/dummy/config/environment.rb +5 -0
  34. data/spec/dummy/config/environments/development.rb +41 -0
  35. data/spec/dummy/config/environments/production.rb +79 -0
  36. data/spec/dummy/config/environments/test.rb +42 -0
  37. data/spec/dummy/config/initializers/assets.rb +11 -0
  38. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  40. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/spec/dummy/config/initializers/friendly_id.rb +88 -0
  42. data/spec/dummy/config/initializers/inflections.rb +16 -0
  43. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  44. data/spec/dummy/config/initializers/session_store.rb +3 -0
  45. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/dummy/config/locales/en.yml +23 -0
  47. data/spec/dummy/config/routes.rb +56 -0
  48. data/spec/dummy/config/secrets.yml +22 -0
  49. data/spec/dummy/db/development.sqlite3 +0 -0
  50. data/spec/dummy/db/migrate/20150220064247_create_friendly_id_slugs.rb +15 -0
  51. data/spec/dummy/db/schema.rb +93 -0
  52. data/spec/dummy/db/test.sqlite3 +0 -0
  53. data/spec/dummy/log/development.log +256 -0
  54. data/spec/dummy/log/test.log +21878 -0
  55. data/spec/dummy/public/404.html +67 -0
  56. data/spec/dummy/public/422.html +67 -0
  57. data/spec/dummy/public/500.html +66 -0
  58. data/spec/dummy/public/favicon.ico +0 -0
  59. data/spec/dummy/test.sqlite3 +0 -0
  60. data/spec/models/group_spec.rb +94 -0
  61. data/spec/models/permission_spec.rb +49 -0
  62. data/spec/models/role_spec.rb +113 -0
  63. data/spec/models/user_spec.rb +139 -0
  64. data/spec/rails_helper.rb +57 -0
  65. data/spec/schema.rb +103 -0
  66. data/spec/spec_helper.rb +85 -0
  67. data/spec/support/factories.rb +54 -0
  68. data/spec/support/model_classes.rb +36 -0
  69. data/spec/support/setup.rb +48 -0
  70. data/spec/test.sqlite3 +0 -0
  71. metadata +294 -0
@@ -0,0 +1,139 @@
1
+ require 'rails_helper'
2
+ require 'support/model_classes'
3
+ require 'support/factories'
4
+ require 'support/setup'
5
+ require 'pry'
6
+
7
+ describe "Test the user model mixin" do
8
+
9
+ context "Fail to find a user" do
10
+ it "should return nil on a user not existing" do
11
+ User.find_by_first_name('sally').should == nil
12
+ end
13
+ end
14
+
15
+ context "Validation checks" do
16
+ it "Validate a valid user object" do
17
+ FactoryGirl.build(:user).should be_valid
18
+ end
19
+
20
+ it "should not be valid if wong user name length" do
21
+ FactoryGirl.build(:user, :user_name => 'gg').should_not be_valid
22
+ end
23
+
24
+ it "should not be valid if missing password" do
25
+ FactoryGirl.build(:user, :password => '').should_not be_valid
26
+ end
27
+
28
+ it "should not be valid if wong password length" do
29
+ FactoryGirl.build(:user, :password => 'ggg').should_not be_valid
30
+ end
31
+
32
+ it "should not be valid if wong email" do
33
+ FactoryGirl.build(:user, :email => 'ggg.com').should_not be_valid
34
+ end
35
+
36
+ it "should validate a user based on login credentials" do
37
+ @user = FactoryGirl.create(:user)
38
+ @login = FactoryGirl.build(:user)
39
+ User.authenticate_user(@user.user_name, @login.password).should == @user
40
+ end
41
+
42
+ it "should fail on password being wrong" do
43
+ @user = FactoryGirl.create(:user)
44
+ @login = FactoryGirl.build(:user)
45
+ User.authenticate_user(@user.user_name, "skfdjhgsdkjfhg").should == nil
46
+ end
47
+
48
+ it "should hash my password" do
49
+ @user = FactoryGirl.create(:user)
50
+ @login = FactoryGirl.build(:user)
51
+ @user.password.should == BCrypt::Engine.hash_secret(@login.password, @user.salt)
52
+ end
53
+ end
54
+
55
+ context "Assign users groups, roles and permissions" do
56
+ before(:each) do
57
+ setup_group_role_permissions_relations_for_memeber
58
+ end
59
+
60
+ it "should assign a group to a user" do
61
+ @user.add_group = @group.group_name
62
+ @user.has_group?(@group.group_name).should_not eql false
63
+ end
64
+
65
+ it "should have a role for a user who belongs to a group" do
66
+ @user.add_group_membership(@group, @role)
67
+ @user.groups.find(@group.group_name.parameterize).has_role?(@role.role_name).should_not eql false
68
+ end
69
+
70
+ it "should have a permission for a role when a user who belongs to a group that has that role" do
71
+ @user.add_group = @group.group_name
72
+ @user.groups.find(@group.group_name.parameterize).roles.find(@role.role_name.parameterize).has_permission?(@permission.permission_name)
73
+ end
74
+ end
75
+
76
+ context "Get a Group and Roles and Permissions" do
77
+ before(:each) do
78
+ setup_group_role_permissions_relations_for_memeber
79
+ end
80
+
81
+ it "should return one group a user belongs to" do
82
+ @user.groups.nil?.should eql false
83
+ end
84
+
85
+ it "should return roles for a user belongs to a group" do
86
+ @user.roles.empty?.should eql false
87
+ end
88
+
89
+ it "should return permissions for a user who has a role" do
90
+ @user.permissions.empty?.should eql false
91
+ end
92
+ end
93
+
94
+ context "Concerns" do
95
+ before(:each) do
96
+ setup_group_role_permissions_relations_for_memeber
97
+ end
98
+
99
+ it "should get a set of associated groups" do
100
+ @user.group_names.should_not be_empty
101
+ end
102
+
103
+ it "should get a set of associated roles" do
104
+ @user.role_names.should_not be_empty
105
+ end
106
+
107
+ it "should get unique permissions" do
108
+ @user.uniq_permissions.should_not be_empty
109
+ end
110
+
111
+ it "should get all permissions" do
112
+ @user.permissions.should_not be_empty
113
+ end
114
+
115
+ it "should return true when the role belongs to the group and the user has both" do
116
+ @user.has_role_for_group?('Member', 'Member').should eql true
117
+ end
118
+
119
+ it "should get a role from a group association" do
120
+ group = Group.find('member')
121
+ @user.get_role_from_group_membership(group.id)
122
+ end
123
+
124
+ it "should remove a group membership association" do
125
+ @user.remove_group_membership('Member')
126
+ @user.has_group?('Member').should eql false
127
+ end
128
+
129
+ it "should get the first role" do
130
+ @user.role.should eql 'Member'
131
+ end
132
+
133
+ it "should get the first group" do
134
+ @user.group.should eql 'Member'
135
+ end
136
+
137
+ end
138
+
139
+ end
@@ -0,0 +1,57 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require 'spec_helper'
4
+ require File.expand_path("../dummy/config/environment", __FILE__)
5
+ require 'rspec/rails'
6
+ require 'factory_girl_rails'
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.sqlite3')
9
+ load(File.dirname(__FILE__) + "/schema.rb")
10
+
11
+ # Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
12
+
13
+ # Add additional requires below this line. Rails is not loaded until this point!
14
+
15
+ # Requires supporting ruby files with custom matchers and macros, etc, in
16
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
17
+ # run as spec files by default. This means that files in spec/support that end
18
+ # in _spec.rb will both be required and run as specs, causing the specs to be
19
+ # run twice. It is recommended that you do not name files matching this glob to
20
+ # end with _spec.rb. You can configure this pattern with the --pattern
21
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
22
+ #
23
+ # The following line is provided for convenience purposes. It has the downside
24
+ # of increasing the boot-up time by auto-requiring all files in the support
25
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
26
+ # require only the support files necessary.
27
+ #
28
+ # Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
29
+
30
+ # Checks for pending migrations before tests are run.
31
+ # If you are not using ActiveRecord, you can remove this line.
32
+ ActiveRecord::Migration.maintain_test_schema!
33
+
34
+ RSpec.configure do |config|
35
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
36
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
37
+
38
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
39
+ # examples within a transaction, remove the following line or assign false
40
+ # instead of true.
41
+ config.use_transactional_fixtures = true
42
+
43
+ # RSpec Rails can automatically mix in different behaviours to your tests
44
+ # based on their file location, for example enabling you to call `get` and
45
+ # `post` in specs under `spec/controllers`.
46
+ #
47
+ # You can disable this behaviour by removing the line below, and instead
48
+ # explicitly tag your specs with their type, e.g.:
49
+ #
50
+ # RSpec.describe UsersController, :type => :controller do
51
+ # # ...
52
+ # end
53
+ #
54
+ # The different available types are documented in the features, such as in
55
+ # https://relishapp.com/rspec/rspec-rails/docs
56
+ config.infer_spec_type_from_file_location!
57
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,103 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20150122213141) do
15
+
16
+ # These are extensions that must be enabled in order to support this database
17
+ enable_extension "plpgsql"
18
+
19
+ create_table "friendly_id_slugs", force: true do |t|
20
+ t.string "slug", null: false
21
+ t.integer "sluggable_id", null: false
22
+ t.string "sluggable_type", limit: 50
23
+ t.string "scope"
24
+ t.datetime "created_at"
25
+ end
26
+
27
+ add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true, using: :btree
28
+ add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", using: :btree
29
+ add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree
30
+ add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree
31
+
32
+
33
+ create_table "group_memberships", force: true do |t|
34
+ t.integer "user_id"
35
+ t.integer "group_id"
36
+ t.integer "role_id"
37
+ t.integer "_v", limit: 8, default: 1
38
+ end
39
+
40
+ create_table "groups", force: true do |t|
41
+ t.string "group_name"
42
+ t.string "slug"
43
+ t.boolean "can_alter", default: true
44
+ t.boolean "can_delete", default: true
45
+ t.integer "_v", limit: 8, default: 1
46
+ end
47
+
48
+ add_index "groups", ["slug"], name: "index_groups_on_slug", using: :btree
49
+
50
+ create_table "groups_roles", force: true do |t|
51
+ t.integer "group_id"
52
+ t.integer "role_id"
53
+ t.integer "_v", limit: 8, default: 1
54
+ end
55
+
56
+ create_table "permissions", force: true do |t|
57
+ t.string "permission_name"
58
+ t.string "slug"
59
+ t.datetime "created_at"
60
+ t.datetime "updated_at"
61
+ t.boolean "can_alter", default: true
62
+ t.boolean "can_delete", default: true
63
+ end
64
+
65
+ add_index "permissions", ["slug"], name: "index_permissions_on_slug", using: :btree
66
+
67
+ create_table "roles", force: true do |t|
68
+ t.string "role_name"
69
+ t.integer "_v", limit: 8
70
+ t.datetime "created_at"
71
+ t.datetime "updated_at"
72
+ t.string "slug"
73
+ t.boolean "can_delete", default: true
74
+ t.boolean "can_alter", default: true
75
+ end
76
+
77
+ add_index "roles", ["slug"], name: "index_roles_on_slug", using: :btree
78
+
79
+ create_table "roles_permissions", force: true do |t|
80
+ t.integer "role_id"
81
+ t.integer "permission_id"
82
+ t.integer "_v", limit: 8, default: 1
83
+ end
84
+
85
+ create_table "users", force: true do |t|
86
+ t.string "first_name"
87
+ t.string "last_name"
88
+ t.string "user_name"
89
+ t.string "email"
90
+ t.string "password"
91
+ t.string "salt"
92
+ t.integer "_v", limit: 8
93
+ t.datetime "created_at"
94
+ t.datetime "updated_at"
95
+ t.string "auth_token"
96
+ t.string "slug"
97
+ t.string "password_reset_token"
98
+ t.datetime "password_reset_timestamp"
99
+ end
100
+
101
+ add_index "users", ["slug"], name: "index_users_on_slug", using: :btree
102
+
103
+ end
@@ -0,0 +1,85 @@
1
+ # This file was generated by the `rails generate rspec:install` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # Many RSpec users commonly either run the entire suite or an individual
59
+ # file, and it's useful to allow more verbose output when running an
60
+ # individual spec file.
61
+ if config.files_to_run.one?
62
+ # Use the documentation formatter for detailed output,
63
+ # unless a formatter has already been configured
64
+ # (e.g. via a command-line flag).
65
+ config.default_formatter = 'doc'
66
+ end
67
+
68
+ # Print the 10 slowest examples and example groups at the
69
+ # end of the spec run, to help surface which specs are running
70
+ # particularly slow.
71
+ config.profile_examples = 10
72
+
73
+ # Run specs in random order to surface order dependencies. If you find an
74
+ # order dependency and want to debug it, you can fix the order by providing
75
+ # the seed, which is printed after each run.
76
+ # --seed 1234
77
+ config.order = :random
78
+
79
+ # Seed global randomization in this process using the `--seed` CLI option.
80
+ # Setting this allows you to use `--seed` to deterministically reproduce
81
+ # test failures related to randomization by passing the same `--seed` value
82
+ # as the one that triggered the failure.
83
+ Kernel.srand config.seed
84
+ =end
85
+ end
@@ -0,0 +1,54 @@
1
+ FactoryGirl.define do
2
+ sequence :user_email do |n|
3
+ "user#{n}@example.com"
4
+ end
5
+
6
+ # Allows for multiple user names
7
+ sequence :user_name do |n|
8
+ "user#{n}"
9
+ end
10
+
11
+ sequence :permission_name do |n|
12
+ "can_read#{n}"
13
+ end
14
+
15
+ sequence :role_name do |n|
16
+ "Member#{n}"
17
+ end
18
+
19
+
20
+ factory :user, :class => User do
21
+ first_name 'Adam'
22
+ last_name 'Something'
23
+ user_name {generate :user_name}
24
+ email {generate :user_email}
25
+ password 'somePasswordThat_Is$ecure10!'
26
+ end
27
+
28
+ factory :admin, :class => User do
29
+ first_name "Sample Admin"
30
+ email "a@gmail.com"
31
+ user_name "Admin_User_Name"
32
+ password "admin_Password10985"
33
+ end
34
+
35
+ factory :member_role, :class => Role do
36
+ role_name "Member"
37
+ end
38
+
39
+ factory :admin_role, :class => Role do
40
+ role_name "Administrator"
41
+ end
42
+
43
+ factory :administrator_group, :class => Group do
44
+ group_name "Administrator"
45
+ end
46
+
47
+ factory :member_group, :class => Group do
48
+ group_name "Member"
49
+ end
50
+
51
+ factory :can_read, :class => Permission do
52
+ permission_name {generate :permission_name}
53
+ end
54
+ end
@@ -0,0 +1,36 @@
1
+ require 'core_models/models/user'
2
+ require 'core_models/models/group'
3
+ require 'core_models/models/group_membership'
4
+ require 'core_models/models/groups_role'
5
+ require 'core_models/models/permission'
6
+ require 'core_models/models/role'
7
+ require 'core_models/models/roles_permission'
8
+
9
+
10
+ class User < ActiveRecord::Base
11
+ include CoreModels::Models::User
12
+ end
13
+
14
+ class Group < ActiveRecord::Base
15
+ include CoreModels::Models::Group
16
+ end
17
+
18
+ class GroupMembership < ActiveRecord::Base
19
+ include CoreModels::Models::GroupMembership
20
+ end
21
+
22
+ class GroupsRole < ActiveRecord::Base
23
+ include CoreModels::Models::GroupsRole
24
+ end
25
+
26
+ class Permission < ActiveRecord::Base
27
+ include CoreModels::Models::Permission
28
+ end
29
+
30
+ class Role < ActiveRecord::Base
31
+ include CoreModels::Models::Role
32
+ end
33
+
34
+ class RolesPermission < ActiveRecord::Base
35
+ include CoreModels::Models::RolesPermission
36
+ end