enja 0.1.0

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.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +50 -0
  3. data/Rakefile +6 -0
  4. data/lib/enja.rb +36 -0
  5. data/lib/enja/railtie.rb +9 -0
  6. data/lib/enja/version.rb +3 -0
  7. data/lib/generators/enja/USAGE +9 -0
  8. data/lib/generators/enja/helper.rb +61 -0
  9. data/lib/generators/enja/install_generator.rb +39 -0
  10. data/lib/generators/enja/templates/migration.erb +6 -0
  11. data/spec/enja_spec.rb +7 -0
  12. data/spec/model_spec.rb +66 -0
  13. data/spec/rails/rails-5.1.2/README.md +24 -0
  14. data/spec/rails/rails-5.1.2/Rakefile +6 -0
  15. data/spec/rails/rails-5.1.2/app/assets/config/manifest.js +3 -0
  16. data/spec/rails/rails-5.1.2/app/assets/javascripts/application.js +15 -0
  17. data/spec/rails/rails-5.1.2/app/assets/javascripts/cable.js +13 -0
  18. data/spec/rails/rails-5.1.2/app/assets/stylesheets/application.css +15 -0
  19. data/spec/rails/rails-5.1.2/app/channels/application_cable/channel.rb +4 -0
  20. data/spec/rails/rails-5.1.2/app/channels/application_cable/connection.rb +4 -0
  21. data/spec/rails/rails-5.1.2/app/controllers/application_controller.rb +3 -0
  22. data/spec/rails/rails-5.1.2/app/helpers/application_helper.rb +2 -0
  23. data/spec/rails/rails-5.1.2/app/jobs/application_job.rb +2 -0
  24. data/spec/rails/rails-5.1.2/app/mailers/application_mailer.rb +4 -0
  25. data/spec/rails/rails-5.1.2/app/models/application_record.rb +3 -0
  26. data/spec/rails/rails-5.1.2/app/models/user.rb +3 -0
  27. data/spec/rails/rails-5.1.2/app/views/layouts/application.html.erb +14 -0
  28. data/spec/rails/rails-5.1.2/app/views/layouts/mailer.html.erb +13 -0
  29. data/spec/rails/rails-5.1.2/app/views/layouts/mailer.text.erb +1 -0
  30. data/spec/rails/rails-5.1.2/bin/bundle +3 -0
  31. data/spec/rails/rails-5.1.2/bin/rails +4 -0
  32. data/spec/rails/rails-5.1.2/bin/rake +4 -0
  33. data/spec/rails/rails-5.1.2/bin/setup +38 -0
  34. data/spec/rails/rails-5.1.2/bin/update +29 -0
  35. data/spec/rails/rails-5.1.2/bin/yarn +11 -0
  36. data/spec/rails/rails-5.1.2/config.ru +5 -0
  37. data/spec/rails/rails-5.1.2/config/application.rb +31 -0
  38. data/spec/rails/rails-5.1.2/config/boot.rb +3 -0
  39. data/spec/rails/rails-5.1.2/config/cable.yml +10 -0
  40. data/spec/rails/rails-5.1.2/config/database.yml +25 -0
  41. data/spec/rails/rails-5.1.2/config/environment.rb +5 -0
  42. data/spec/rails/rails-5.1.2/config/environments/development.rb +47 -0
  43. data/spec/rails/rails-5.1.2/config/environments/production.rb +83 -0
  44. data/spec/rails/rails-5.1.2/config/environments/test.rb +42 -0
  45. data/spec/rails/rails-5.1.2/config/initializers/application_controller_renderer.rb +6 -0
  46. data/spec/rails/rails-5.1.2/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/rails/rails-5.1.2/config/initializers/cookies_serializer.rb +5 -0
  48. data/spec/rails/rails-5.1.2/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/spec/rails/rails-5.1.2/config/initializers/inflections.rb +16 -0
  50. data/spec/rails/rails-5.1.2/config/initializers/mime_types.rb +4 -0
  51. data/spec/rails/rails-5.1.2/config/initializers/wrap_parameters.rb +14 -0
  52. data/spec/rails/rails-5.1.2/config/locales/en.yml +33 -0
  53. data/spec/rails/rails-5.1.2/config/puma.rb +56 -0
  54. data/spec/rails/rails-5.1.2/config/routes.rb +3 -0
  55. data/spec/rails/rails-5.1.2/config/secrets.yml +32 -0
  56. data/spec/rails/rails-5.1.2/db/migrate/20170710091815_create_users.rb +8 -0
  57. data/spec/rails/rails-5.1.2/db/migrate/20170710091816_add_role_to_user_resources.rb +6 -0
  58. data/spec/rails/rails-5.1.2/db/schema.rb +22 -0
  59. data/spec/rails/rails-5.1.2/db/seeds.rb +7 -0
  60. data/spec/rails/rails-5.1.2/db/test.sqlite3 +0 -0
  61. data/spec/rails/rails-5.1.2/log/test.log +730 -0
  62. data/spec/rails/rails-5.1.2/package.json +5 -0
  63. data/spec/rails/rails-5.1.2/public/404.html +67 -0
  64. data/spec/rails/rails-5.1.2/public/422.html +67 -0
  65. data/spec/rails/rails-5.1.2/public/500.html +66 -0
  66. data/spec/rails/rails-5.1.2/public/apple-touch-icon-precomposed.png +0 -0
  67. data/spec/rails/rails-5.1.2/public/apple-touch-icon.png +0 -0
  68. data/spec/rails/rails-5.1.2/public/favicon.ico +0 -0
  69. data/spec/rails/rails-5.1.2/public/robots.txt +1 -0
  70. data/spec/spec_helper.rb +45 -0
  71. data/spec/support/rails_template.rb +9 -0
  72. metadata +203 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f36a7cf83cb08100e09ae42b5a52bdbae2f3e7d
4
+ data.tar.gz: bf152500009aa5a9814cf7b34f5d51ef6dd0493d
5
+ SHA512:
6
+ metadata.gz: 38c765a07516f271a88ae997eb91ec99228b5a80d6cecdea3f0682470148175f2dc6518b9635d14ef1d7f680b08a5abef59134a7e99aba58ff740b59935c5e28
7
+ data.tar.gz: 4f0e9dd1d08d55a028e461ffcb8f6fc5c3b2be6d80c8fa4dffdf50a9d4927495d8f94f741e60acf667d07478efb5429fd3de1ec2e713df3fbd0ecf92f09b9127
@@ -0,0 +1,50 @@
1
+ # Enja
2
+
3
+ Easily role definition depend on `enumerize` gem for Rails.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'enja'
9
+ ```
10
+
11
+ Optionally, you can run the generator, which will set up a role field with some useful defaults for you:
12
+
13
+ ```bash
14
+ $ bin/rails g enja:install
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Call `define_role_method` in your resource of user model (`User`, `AdminUser` or `Member` etc):
20
+
21
+ ```ruby
22
+ class User < ApplicationRecord
23
+ define_role_method :role, in: [:guest, :staff, :admin], default: :guest
24
+ end
25
+ ```
26
+
27
+ Then it provides some convenient methods for implement role based system:
28
+
29
+ ```ruby
30
+ user = User.new(role: :guest)
31
+
32
+ # == Predicate Methods
33
+ user.guest? # true
34
+ user.staff? # false
35
+ user.admin? # false
36
+
37
+ # == Update Methods
38
+ user.guest! # => :guest
39
+ user.staff! # => :staff
40
+ user.admin! # => :admin
41
+
42
+ # == Scopes
43
+ User.guest.all # User.where(role: :guest).all
44
+ User.staff.all # User.where(role: :staff).all
45
+ User.admin.all # User.where(role: :admin).all
46
+ ```
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ require "enumerize"
2
+
3
+ require "enja/railtie"
4
+ require "enja/version"
5
+
6
+ module Enja
7
+ def self.included(klass)
8
+ klass.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def define_role_field(attribute_name = :role, options = {})
13
+ enumerize_options = enja_default_options.merge!(options)
14
+
15
+ extend ::Enumerize
16
+ enumerize(attribute_name.to_sym, enumerize_options)
17
+ enumerize_options[:in].each do |role|
18
+ scope(role, -> { where(attribute_name => role) })
19
+ define_method(:"#{role}!") { update!(attribute_name => role ) }
20
+ end
21
+
22
+ validates(attribute_name, presence: true)
23
+ end
24
+
25
+ private
26
+
27
+ def enja_default_options
28
+ {
29
+ in: %i(guest staff admin),
30
+ default: :guest,
31
+ predicates: true,
32
+ scope: true,
33
+ }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ module Enja
2
+ class Railtie < Rails::Railtie
3
+ initializer "enja" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include Enja
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Enja
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generates the necessary files to get you up and running with Enja gem
3
+
4
+ Examples:
5
+ rails g enja:install
6
+
7
+ This will generate the core migration file, the initializer file and the 'User' model class.
8
+
9
+ rails g enja:install --model AdminUser --attribute state
@@ -0,0 +1,61 @@
1
+ module Enja
2
+ module Generators
3
+ module Helper
4
+ extend ActiveSupport::Concern
5
+
6
+ private
7
+
8
+ def model_class_name
9
+ options.key?(:model) ? options[:model].classify : "User"
10
+ end
11
+
12
+ def model_file_path
13
+ model_name.underscore
14
+ end
15
+
16
+ def model_path
17
+ @model_path ||= File.join("app", "models", "#{model_file_path}.rb")
18
+ end
19
+
20
+ def namespace
21
+ Rails::Generators.namespace if Rails::Generators.respond_to?(:namespace)
22
+ end
23
+
24
+ def namespaced?
25
+ !!namespace
26
+ end
27
+
28
+ def model_name
29
+ if namespaced?
30
+ [namespace.to_s] + [model_class_name]
31
+ else
32
+ [model_class_name]
33
+ end.join("::")
34
+ end
35
+
36
+ def attribute_name
37
+ options.key?(:attribute) ? options[:attribute].underscore : "role"
38
+ end
39
+
40
+ def migration_class_name
41
+ if Rails::VERSION::MAJOR >= 5
42
+ "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
43
+ else
44
+ "ActiveRecord::Migration"
45
+ end
46
+ end
47
+
48
+ class_methods do
49
+ # Define the next_migration_number method (necessary for the migration_template method to work)
50
+ def next_migration_number(dirname)
51
+ if ActiveRecord::Base.timestamped_migrations
52
+ sleep 1 # make sure each time we get a different timestamp
53
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
54
+ else
55
+ format("%.3d", (current_migration_number(dirname) + 1))
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,39 @@
1
+ require "rails/generators/migration"
2
+ require "generators/enja/helper"
3
+
4
+ module Enja
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ include Enja::Generators::Helper
9
+
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ class_option :model, optional: true,
13
+ type: :string,
14
+ banner: "model",
15
+ desc: "Specify the model class name if you will use anything other than `User`",
16
+ default: "User"
17
+
18
+ class_option :attribute, optional: true,
19
+ type: :string,
20
+ banner: "attribute",
21
+ desc: "Specify the attribute name if you will use anything other than `role`",
22
+ default: "role"
23
+
24
+ def prepare_resource_model
25
+ generate "model", "#{model_class_name}" unless model_class_name.safe_constantize
26
+ end
27
+
28
+ def prepare_migration
29
+ migration_template "migration.erb", "db/migrate/add_role_to_user_resources.rb", migration_class_name: migration_class_name
30
+ end
31
+
32
+ def inject_resource_model
33
+ inject_into_file model_path, after: "class #{model_class_name} < ApplicationRecord\n" do
34
+ " define_role_field :#{attribute_name}, in: [:guest, :staff, :admin], default: :guest\n"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ class AddRoleToUserResources < <%= migration_class_name %>
2
+ def change
3
+ add_column :<%= model_class_name.tableize.tr('/', '_') %>, :<%= attribute_name %>, :string, null: false, default: ""
4
+ add_index :<%= model_class_name.tableize.tr('/', '_') %>, :<%= attribute_name %>
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Enja do
4
+ it "has a version number" do
5
+ expect(Enja::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe User, type: :model do
4
+ let!(:guest) { described_class.create(role: :guest) }
5
+ let!(:staff) { described_class.create(role: :staff) }
6
+ let!(:admin) { described_class.create(role: :admin) }
7
+
8
+ context 'enumerize' do
9
+ subject { described_class.role }
10
+ it { is_expected.to be_a(Enumerize::Attribute) }
11
+ end
12
+
13
+ context 'scope' do
14
+ describe '.guest' do
15
+ subject { described_class.guest }
16
+ it { is_expected.to include guest }
17
+ it { is_expected.not_to include staff }
18
+ it { is_expected.not_to include admin }
19
+ end
20
+
21
+ describe '.staff' do
22
+ subject { described_class.staff }
23
+ it { is_expected.to include staff }
24
+ it { is_expected.not_to include guest }
25
+ it { is_expected.not_to include admin }
26
+ end
27
+
28
+ describe '.admin' do
29
+ subject { described_class.admin }
30
+ it { is_expected.to include admin }
31
+ it { is_expected.not_to include guest }
32
+ it { is_expected.not_to include staff }
33
+ end
34
+ end
35
+
36
+ context 'validation' do
37
+ it { is_expected.to validate_presence_of(:role) }
38
+ end
39
+
40
+ describe '#guest!' do
41
+ let(:instance) { described_class.create(role: :admin) }
42
+
43
+ subject(:execution) { instance.guest! }
44
+
45
+ it { is_expected.to eq true }
46
+ it { expect { execution }.to change { instance.role }.from('admin').to('guest') }
47
+ end
48
+
49
+ describe '#staff!' do
50
+ let(:instance) { described_class.create(role: :admin) }
51
+
52
+ subject(:execution) { instance.staff! }
53
+
54
+ it { is_expected.to eq true }
55
+ it { expect { execution }.to change { instance.role }.from('admin').to('staff') }
56
+ end
57
+
58
+ describe '#admin!' do
59
+ let(:instance) { described_class.create(role: :guest) }
60
+
61
+ subject(:execution) { instance.admin! }
62
+
63
+ it { is_expected.to eq true }
64
+ it { expect { execution }.to change { instance.role }.from('guest').to('admin') }
65
+ end
66
+ end
@@ -0,0 +1,24 @@
1
+ # README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
5
+ // vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require turbolinks
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the `rails generate channel` command.
3
+ //
4
+ //= require action_cable
5
+ //= require_self
6
+ //= require_tree ./channels
7
+
8
+ (function() {
9
+ this.App || (this.App = {});
10
+
11
+ App.cable = ActionCable.createConsumer();
12
+
13
+ }).call(this);
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
6
+ * vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ApplicationRecord
2
+ define_role_field :role, in: [:guest, :staff, :admin], default: :guest
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails512</title>
5
+ <%= csrf_meta_tags %>
6
+
7
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
8
+ <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
9
+ </head>
10
+
11
+ <body>
12
+ <%= yield %>
13
+ </body>
14
+ </html>