activeadmin_polymorphic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/Gemfile +40 -0
  4. data/Guardfile +8 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +86 -0
  7. data/Rakefile +33 -0
  8. data/activeadmin_polymorphic.gemspec +19 -0
  9. data/app/assets/javascripts/activeadmin_polymorphic.js.coffee +172 -0
  10. data/app/assets/stylesheets/activeadmin_polymorphic.css.sass +25 -0
  11. data/lib/activeadmin_polymorphic/engine.rb +5 -0
  12. data/lib/activeadmin_polymorphic/form_builder.rb +124 -0
  13. data/lib/activeadmin_polymorphic/version.rb +3 -0
  14. data/lib/activeadmin_polymorphic.rb +5 -0
  15. data/spec/rails_helper.rb +154 -0
  16. data/spec/spec_helper.rb +17 -0
  17. data/spec/support/deferred_garbage_collection.rb +19 -0
  18. data/spec/support/detect_rails_version.rb +26 -0
  19. data/spec/support/integration_example_group.rb +31 -0
  20. data/spec/support/jslint.yml +80 -0
  21. data/spec/support/rails_template.rb +104 -0
  22. data/spec/support/rails_template_with_data.rb +59 -0
  23. data/spec/support/templates/cucumber.rb +24 -0
  24. data/spec/support/templates/cucumber_with_reloading.rb +5 -0
  25. data/spec/support/templates/en.yml +8 -0
  26. data/spec/support/templates/policies/active_admin/comment_policy.rb +9 -0
  27. data/spec/support/templates/policies/active_admin/page_policy.rb +18 -0
  28. data/spec/support/templates/policies/application_policy.rb +45 -0
  29. data/spec/support/templates/policies/category_policy.rb +7 -0
  30. data/spec/support/templates/policies/post_policy.rb +15 -0
  31. data/spec/support/templates/policies/store_policy.rb +11 -0
  32. data/spec/support/templates/policies/user_policy.rb +11 -0
  33. data/spec/support/templates/post_decorator.rb +11 -0
  34. data/spec/unit/form_builder_spec.rb +121 -0
  35. data/tasks/test.rake +83 -0
  36. metadata +102 -0
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ module ActiveAdminIntegrationSpecHelper
4
+ extend self
5
+
6
+ def load_defaults!
7
+ ActiveAdmin.unload!
8
+ ActiveAdmin.load!
9
+ ActiveAdmin.register(Article)
10
+ ActiveAdmin.register(Image)
11
+ ActiveAdmin.register(Text)
12
+ # reload_menus!
13
+ end
14
+
15
+ # def reload_menus!
16
+ # ActiveAdmin.application.namespaces.values.each{|n| n.reset_menu! }
17
+ # end
18
+
19
+ # Sometimes we need to reload the routes within
20
+ # the application to test them out
21
+ def reload_routes!
22
+ Rails.application.reload_routes!
23
+ end
24
+
25
+ # Helper method to load resources and ensure that Active Admin is
26
+ # setup with the new configurations.
27
+ #
28
+ # Eg:
29
+ # load_resources do
30
+ # ActiveAdmin.regiser(Post)
31
+ # end
32
+ #
33
+ def load_resources
34
+ ActiveAdmin.unload!
35
+ yield
36
+ reload_menus!
37
+ reload_routes!
38
+ end
39
+
40
+ # Sets up a describe block where you can render controller
41
+ # actions. Uses the Admin::PostsController as the subject
42
+ # for the describe block
43
+ def describe_with_render(*args, &block)
44
+ describe *args do
45
+ include RSpec::Rails::ControllerExampleGroup
46
+ render_views
47
+ # metadata[:behaviour][:describes] = ActiveAdmin.namespaces[:admin].resources['Post'].controller
48
+ module_eval &block
49
+ end
50
+ end
51
+
52
+ def arbre(assigns = {}, helpers = mock_action_view, &block)
53
+ Arbre::Context.new(assigns, helpers, &block)
54
+ end
55
+
56
+ def render_arbre_component(assigns = {}, helpers = mock_action_view, &block)
57
+ arbre(assigns, helpers, &block).children.first
58
+ end
59
+
60
+ # Setup a describe block which uses capybara and rails integration
61
+ # test methods.
62
+ def describe_with_capybara(*args, &block)
63
+ describe *args do
64
+ include RSpec::Rails::IntegrationExampleGroup
65
+ module_eval &block
66
+ end
67
+ end
68
+
69
+ # Returns a fake action view instance to use with our renderers
70
+ def mock_action_view(assigns = {})
71
+ controller = ActionView::TestCase::TestController.new
72
+ ActionView::Base.send :include, ActionView::Helpers
73
+ ActionView::Base.send :include, ActiveAdmin::ViewHelpers
74
+ ActionView::Base.send :include, Rails.application.routes.url_helpers
75
+ ActionView::Base.new(ActionController::Base.view_paths, assigns, controller)
76
+ end
77
+ alias_method :action_view, :mock_action_view
78
+
79
+ # A mock resource to register
80
+ class MockResource
81
+ end
82
+
83
+ def with_translation(translation)
84
+ I18n.backend.store_translations :en, translation
85
+ yield
86
+ ensure
87
+ I18n.backend.reload!
88
+ end
89
+
90
+ end
91
+
92
+ ENV['RAILS_ENV'] = 'test'
93
+ ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{ENV['RAILS']}", __FILE__)
94
+
95
+ # Create the test app if it doesn't exists
96
+ unless File.exists?(ENV['RAILS_ROOT'])
97
+ system 'rake setup'
98
+ end
99
+
100
+ require 'rails'
101
+ require 'active_record'
102
+ require 'active_admin'
103
+ # require 'devise'
104
+ ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
105
+
106
+ require ENV['RAILS_ROOT'] + '/config/environment'
107
+
108
+ require 'rspec/rails'
109
+
110
+ # Setup Some Admin stuff for us to play with
111
+ include ActiveAdminIntegrationSpecHelper
112
+ load_defaults!
113
+ reload_routes!
114
+
115
+ # Disabling authentication in specs so that we don't have to worry about
116
+ # it allover the place
117
+ ActiveAdmin.application.authentication_method = false
118
+ ActiveAdmin.application.current_user_method = false
119
+
120
+ # Don't add asset cache timestamps. Makes it easy to integration
121
+ # test for the presence of an asset file
122
+ ENV["RAILS_ASSET_ID"] = ''
123
+
124
+ RSpec.configure do |config|
125
+ config.use_transactional_fixtures = true
126
+ config.use_instantiated_fixtures = false
127
+ # config.include Devise::TestHelpers, type: :controller
128
+ config.render_views = false
129
+ config.filter_run focus: true
130
+ config.filter_run_excluding skip: true
131
+ config.run_all_when_everything_filtered = true
132
+ end
133
+
134
+ # All RSpec configuration needs to happen before any examples
135
+ # or else it whines.
136
+ require 'integration_example_group'
137
+ RSpec.configure do |c|
138
+ c.include RSpec::Rails::IntegrationExampleGroup, file_path: /\bspec\/requests\//
139
+ # c.include Devise::TestHelpers, type: :controller
140
+ end
141
+
142
+ # improve the performance of the specs suite by not logging anything
143
+ # see http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/
144
+ Rails.logger.level = 4
145
+
146
+
147
+ # Improves performance by forcing the garbage collector to run less often.
148
+ unless ENV['DEFER_GC'] == '0' || ENV['DEFER_GC'] == 'false'
149
+ require 'support/deferred_garbage_collection'
150
+ RSpec.configure do |config|
151
+ config.before(:all) { DeferredGarbageCollection.start }
152
+ config.after(:all) { DeferredGarbageCollection.reconsider }
153
+ end
154
+ end
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH << File.expand_path('../support', __FILE__)
3
+
4
+ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
5
+ require "bundler"
6
+ Bundler.setup
7
+
8
+ require 'detect_rails_version'
9
+ ENV['RAILS'] = detect_rails_version
10
+
11
+ require 'simplecov'
12
+
13
+ SimpleCov.start do
14
+ add_filter 'spec/'
15
+ add_filter 'features/'
16
+ add_filter 'bundle/' # for Travis
17
+ end
@@ -0,0 +1,19 @@
1
+ class DeferredGarbageCollection
2
+
3
+ DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 15.0).to_f
4
+
5
+ @@last_gc_run = Time.now
6
+
7
+ def self.start
8
+ GC.disable
9
+ end
10
+
11
+ def self.reconsider
12
+ if Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
13
+ GC.enable
14
+ GC.start
15
+ GC.disable
16
+ @@last_gc_run = Time.now
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # Detects the current version of Rails that is being used
2
+ #
3
+ #
4
+ RAILS_VERSION_FILE ||= File.expand_path("../../../.rails-version", __FILE__)
5
+
6
+
7
+ def detect_rails_version
8
+ version = version_from_file || ENV['RAILS'] || '4.2.0'
9
+ ensure
10
+ puts "Detected Rails: #{version}" if ENV['DEBUG']
11
+ end
12
+
13
+ def detect_rails_version!
14
+ detect_rails_version or raise "can't find a version of Rails to use!"
15
+ end
16
+
17
+ def version_from_file
18
+ if File.exists?(RAILS_VERSION_FILE)
19
+ version = File.read(RAILS_VERSION_FILE).chomp.strip
20
+ version unless version == ''
21
+ end
22
+ end
23
+
24
+ def write_rails_version(version)
25
+ File.open(RAILS_VERSION_FILE, "w+"){|f| f << version }
26
+ end
@@ -0,0 +1,31 @@
1
+ require 'action_dispatch'
2
+ require 'capybara/rails'
3
+ require 'capybara/dsl'
4
+
5
+ module RSpec
6
+ module Rails
7
+ module IntegrationExampleGroup
8
+ extend ActiveSupport::Concern
9
+
10
+ include ActionDispatch::Integration::Runner
11
+ include RSpec::Rails::TestUnitAssertionAdapter
12
+ include ActionDispatch::Assertions
13
+ include Capybara::DSL
14
+ include RSpec::Matchers
15
+
16
+ def app
17
+ ::Rails.application
18
+ end
19
+
20
+ def last_response
21
+ page
22
+ end
23
+
24
+ included do
25
+ before do
26
+ @router = ::Rails.application.routes
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,80 @@
1
+ # ------------ rake task options ------------
2
+
3
+ # JS files to check by default, if no parameters are passed to rake jslint
4
+ # (you may want to limit this only to your own scripts and exclude any external scripts and frameworks)
5
+
6
+ # this can be overridden by adding 'paths' and 'exclude_paths' parameter to rake command:
7
+ # rake jslint paths=path1,path2,... exclude_paths=library1,library2,...
8
+
9
+ paths:
10
+ - app/assets/javascripts/active_admin/**/*.js
11
+
12
+ exclude_paths:
13
+ - app/assets/javascripts/active_admin/vendor.js
14
+
15
+ # ------------ jslint options ------------
16
+ # see http://www.jslint.com/lint.html#options for more detailed explanations
17
+
18
+ # "enforce" type options (true means potentially more warnings)
19
+
20
+ adsafe: false # true if ADsafe rules should be enforced. See http://www.ADsafe.org/
21
+ bitwise: true # true if bitwise operators should not be allowed
22
+ newcap: true # true if Initial Caps must be used with constructor functions
23
+ eqeqeq: false # true if === should be required (for ALL equality comparisons)
24
+ immed: false # true if immediate function invocations must be wrapped in parens
25
+ nomen: false # true if initial or trailing underscore in identifiers should be forbidden
26
+ onevar: false # true if only one var statement per function should be allowed
27
+ plusplus: false # true if ++ and -- should not be allowed
28
+ regexp: false # true if . and [^...] should not be allowed in RegExp literals
29
+ safe: false # true if the safe subset rules are enforced (used by ADsafe)
30
+ strict: false # true if the ES5 "use strict"; pragma is required
31
+ undef: false # true if variables must be declared before used
32
+ white: false # true if strict whitespace rules apply (see also 'indent' option)
33
+
34
+ # "allow" type options (false means potentially more warnings)
35
+
36
+ cap: false # true if upper case HTML should be allowed
37
+ css: true # true if CSS workarounds should be tolerated
38
+ debug: false # true if debugger statements should be allowed (set to false before going into production)
39
+ es5: false # true if ECMAScript 5 syntax should be allowed
40
+ evil: false # true if eval should be allowed
41
+ forin: true # true if unfiltered 'for in' statements should be allowed
42
+ fragment: true # true if HTML fragments should be allowed
43
+ laxbreak: false # true if statement breaks should not be checked
44
+ on: false # true if HTML event handlers (e.g. onclick="...") should be allowed
45
+ sub: false # true if subscript notation may be used for expressions better expressed in dot notation
46
+
47
+ # other options
48
+
49
+ maxlen: 300 # Maximum line length
50
+ indent: 2 # Number of spaces that should be used for indentation - used only if 'white' option is set
51
+ maxerr: 50 # The maximum number of warnings reported (per file)
52
+ passfail: false # true if the scan should stop on first error (per file)
53
+ # following are relevant only if undef = true
54
+ predef: '' # Names of predefined global variables - comma-separated string or a YAML array
55
+ browser: true # true if the standard browser globals should be predefined
56
+ rhino: false # true if the Rhino environment globals should be predefined
57
+ windows: false # true if Windows-specific globals should be predefined
58
+ widget: false # true if the Yahoo Widgets globals should be predefined
59
+ devel: true # true if functions like alert, confirm, console, prompt etc. are predefined
60
+
61
+
62
+ # ------------ jslint_on_rails custom lint options (switch to true to disable some annoying warnings) ------------
63
+
64
+ # ignores "missing semicolon" warning at the end of a function; this lets you write one-liners
65
+ # like: x.map(function(i) { return i + 1 }); without having to put a second semicolon inside the function
66
+ lastsemic: false
67
+
68
+ # allows you to use the 'new' expression as a statement (without assignment)
69
+ # so you can call e.g. new Ajax.Request(...), new Effect.Highlight(...) without assigning to a dummy variable
70
+ newstat: false
71
+
72
+ # ignores the "Expected an assignment or function call and instead saw an expression" warning,
73
+ # if the expression contains a proper statement and makes sense; this lets you write things like:
74
+ # element && element.show();
75
+ # valid || other || lastChance || alert('OMG!');
76
+ # selected ? show() : hide();
77
+ # although these will still cause a warning:
78
+ # element && link;
79
+ # selected ? 5 : 10;
80
+ statinexp: false
@@ -0,0 +1,104 @@
1
+ # Rails template to build the sample app for specs
2
+
3
+ run "rm Gemfile"
4
+ run "rm -r test"
5
+
6
+ # Create a cucumber database and environment
7
+ copy_file File.expand_path('../templates/cucumber.rb', __FILE__), "config/environments/cucumber.rb"
8
+ copy_file File.expand_path('../templates/cucumber_with_reloading.rb', __FILE__), "config/environments/cucumber_with_reloading.rb"
9
+
10
+ gsub_file 'config/database.yml', /^test:.*\n/, "test: &test\n"
11
+ gsub_file 'config/database.yml', /\z/, "\ncucumber:\n <<: *test\n database: db/cucumber.sqlite3"
12
+ gsub_file 'config/database.yml', /\z/, "\ncucumber_with_reloading:\n <<: *test\n database: db/cucumber.sqlite3"
13
+
14
+ if File.exists? 'config/secrets.yml'
15
+ gsub_file 'config/secrets.yml', /\z/, "\ncucumber:\n secret_key_base: #{'o' * 128}"
16
+ gsub_file 'config/secrets.yml', /\z/, "\ncucumber_with_reloading:\n secret_key_base: #{'o' * 128}"
17
+ end
18
+
19
+
20
+ generate :model, "article title:string body:text --skip-unit-test"
21
+ inject_into_file "app/models/article.rb", %q{
22
+ has_many :sections
23
+ accepts_nested_attributes_for :sections
24
+ }, after: 'class Article < ActiveRecord::Base'
25
+
26
+ generate :model, "image image:string --skip-unit-test"
27
+ inject_into_file "app/models/image.rb", %q{
28
+ has_many :sections
29
+ }, after: 'class Image < ActiveRecord::Base'
30
+
31
+ generate :model, "text body:string --skip-unit-test"
32
+ inject_into_file "app/models/text.rb", %q{
33
+ has_many :sections
34
+ }, after: 'class Text < ActiveRecord::Base'
35
+
36
+ generate :model, "section article:belongs_to sectionable:belongs_to{polymorphic} position:integer"
37
+ inject_into_file "app/models/section.rb", %q{
38
+ belongs_to :sectionable, polymorphic: true
39
+ belongs_to :article
40
+ accepts_nested_attributes_for :sectionable
41
+ }, after: 'class Section < ActiveRecord::Base'
42
+
43
+ # Configure default_url_options in test environment
44
+ inject_into_file "config/environments/test.rb", " config.action_mailer.default_url_options = { host: 'example.com' }\n", after: "config.cache_classes = true\n"
45
+
46
+ # Add our local Active Admin to the load path
47
+ inject_into_file "config/environment.rb", "\n$LOAD_PATH.unshift('#{File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib'))}')\nrequire \"active_admin\"\n", after: "require File.expand_path('../application', __FILE__)"
48
+ #inject_into_file "config/application.rb", "\nrequire 'devise'\n", after: "require 'rails/all'"
49
+
50
+ # Force strong parameters to raise exceptions
51
+ inject_into_file 'config/application.rb', "\n\n config.action_controller.action_on_unpermitted_parameters = :raise if Rails::VERSION::MAJOR == 4\n\n", after: 'class Application < Rails::Application'
52
+
53
+ # Add some translations
54
+ append_file "config/locales/en.yml", File.read(File.expand_path('../templates/en.yml', __FILE__))
55
+
56
+ # Add predefined admin resources
57
+ directory File.expand_path('../templates/admin', __FILE__), "app/admin"
58
+
59
+ # Add predefined policies
60
+ directory File.expand_path('../templates/policies', __FILE__), 'app/policies'
61
+
62
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
63
+
64
+ generate 'active_admin:install --skip-users'
65
+
66
+ inject_into_file "config/routes.rb", "\n root to: redirect('/admin')", after: /.*::Application.routes.draw do/
67
+ remove_file "public/index.html" if File.exists? "public/index.html"
68
+
69
+ # Devise master doesn't set up its secret key on Rails 4.1
70
+ # https://github.com/plataformatec/devise/issues/2554
71
+ # gsub_file 'config/initializers/devise.rb', /# config.secret_key =/, 'config.secret_key ='
72
+
73
+ rake "db:migrate db:test:prepare"
74
+ run "/usr/bin/env RAILS_ENV=cucumber rake db:migrate"
75
+
76
+ if ENV['INSTALL_PARALLEL']
77
+ inject_into_file 'config/database.yml', "<%= ENV['TEST_ENV_NUMBER'] %>", after: 'test.sqlite3'
78
+ inject_into_file 'config/database.yml', "<%= ENV['TEST_ENV_NUMBER'] %>", after: 'cucumber.sqlite3', force: true
79
+
80
+ # Note: this is hack!
81
+ # Somehow, calling parallel_tests tasks from Rails generator using Thor does not work ...
82
+ # RAILS_ENV variable never makes it to parallel_tests tasks.
83
+ # We need to call these tasks in the after set up hook in order to creates cucumber DBs + run migrations on test & cucumber DBs
84
+ create_file 'lib/tasks/parallel.rake', %q{
85
+ namespace :parallel do
86
+ def run_in_parallel(cmd, options)
87
+ count = "-n #{options[:count]}" if options[:count]
88
+ executable = 'parallel_test'
89
+ command = "#{executable} --exec '#{cmd}' #{count} #{'--non-parallel' if options[:non_parallel]}"
90
+ abort unless system(command)
91
+ end
92
+
93
+ desc "create cucumber databases via db:create --> parallel:create_cucumber_db[num_cpus]"
94
+ task :create_cucumber_db, :count do |t, args|
95
+ run_in_parallel("rake db:create RAILS_ENV=cucumber", args)
96
+ end
97
+
98
+ desc "load dumped schema for cucumber databases"
99
+ task :load_schema_cucumber_db, :count do |t,args|
100
+ run_in_parallel("rake db:schema:load RAILS_ENV=cucumber", args)
101
+ end
102
+ end
103
+ }
104
+ end
@@ -0,0 +1,59 @@
1
+ # Use the default
2
+ apply File.expand_path("../rails_template.rb", __FILE__)
3
+
4
+ # Register Active Admin controllers
5
+ %w{ Post User Category }.each do |type|
6
+ generate :'active_admin:resource', type
7
+ end
8
+
9
+ scopes = <<-EOF
10
+ scope :all, default: true
11
+
12
+ scope :drafts do |posts|
13
+ posts.where(["published_at IS NULL"])
14
+ end
15
+
16
+ scope :scheduled do |posts|
17
+ posts.where(["posts.published_at IS NOT NULL AND posts.published_at > ?", Time.now.utc])
18
+ end
19
+
20
+ scope :published do |posts|
21
+ posts.where(["posts.published_at IS NOT NULL AND posts.published_at < ?", Time.now.utc])
22
+ end
23
+
24
+ scope :my_posts do |posts|
25
+ posts.where(author_id: current_admin_user.id)
26
+ end
27
+ EOF
28
+ inject_into_file 'app/admin/posts.rb', scopes , after: "ActiveAdmin.register Post do\n"
29
+
30
+ # Setup some default data
31
+ append_file "db/seeds.rb", <<-EOF
32
+ users = ["Jimi Hendrix", "Jimmy Page", "Yngwie Malmsteen", "Eric Clapton", "Kirk Hammett"].collect do |name|
33
+ first, last = name.split(" ")
34
+ User.create! first_name: first,
35
+ last_name: last,
36
+ username: [first,last].join('-').downcase,
37
+ age: rand(80)
38
+ end
39
+
40
+ categories = ["Rock", "Pop Rock", "Alt-Country", "Blues", "Dub-Step"].collect do |name|
41
+ Category.create! name: name
42
+ end
43
+
44
+ published_at_values = [Time.now.utc - 5.days, Time.now.utc - 1.day, nil, Time.now.utc + 3.days]
45
+
46
+ 1_000.times do |i|
47
+ user = users[i % users.size]
48
+ cat = categories[i % categories.size]
49
+ published_at = published_at_values[i % published_at_values.size]
50
+ Post.create title: "Blog Post \#{i}",
51
+ body: "Blog post \#{i} is written by \#{user.username} about \#{cat.name}",
52
+ category: cat,
53
+ published_at: published_at,
54
+ author: user,
55
+ starred: true
56
+ end
57
+ EOF
58
+
59
+ rake 'db:seed'
@@ -0,0 +1,24 @@
1
+ require File.expand_path('config/environments/test', Rails.root)
2
+
3
+ # rails/railties/lib/rails/test_help.rb aborts if the environment is not 'test'. (Rails 3.0.0.beta3)
4
+ # We can't run Cucumber/RSpec/Test_Unit tests in different environments then.
5
+ #
6
+ # For now, I patch StringInquirer so that Rails.env.test? returns true when Rails.env is 'test' or 'cucumber'
7
+ #
8
+ # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4458-rails-should-allow-test-to-run-in-cucumber-environment
9
+ module ActiveSupport
10
+ class StringInquirer < String
11
+ def method_missing(method_name, *arguments)
12
+ if method_name.to_s[-1,1] == "?"
13
+ test_string = method_name.to_s[0..-2]
14
+ if test_string == 'test'
15
+ self == 'test' or self == 'cucumber'
16
+ else
17
+ self == test_string
18
+ end
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path('config/environments/cucumber', Rails.root)
2
+
3
+ Rails.application.class.configure do
4
+ config.cache_classes = false
5
+ end
@@ -0,0 +1,8 @@
1
+ # Sample translations used to test ActiveAdmin's I18n integration.
2
+ activerecord:
3
+ models:
4
+ store:
5
+ one: Bookstore
6
+ other: Bookstores
7
+ active_admin:
8
+ download: "Download this:"
@@ -0,0 +1,9 @@
1
+ module ActiveAdmin
2
+ class CommentPolicy < ApplicationPolicy
3
+ class Scope < Struct.new(:user, :scope)
4
+ def resolve
5
+ scope
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module ActiveAdmin
2
+ class PagePolicy < ApplicationPolicy
3
+ class Scope < Struct.new(:user, :scope)
4
+ def resolve
5
+ scope
6
+ end
7
+ end
8
+
9
+ def show?
10
+ case record.name
11
+ when "Dashboard"
12
+ true
13
+ else
14
+ false
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ class ApplicationPolicy
2
+ attr_reader :user, :record
3
+
4
+ def initialize(user, record)
5
+ @user = user
6
+ @record = record
7
+ end
8
+
9
+ def index?
10
+ true
11
+ end
12
+
13
+ def show?
14
+ scope.where(:id => record.id).exists?
15
+ end
16
+
17
+ def new?
18
+ create?
19
+ end
20
+
21
+ def create?
22
+ true
23
+ end
24
+
25
+ def edit?
26
+ update?
27
+ end
28
+
29
+ def update?
30
+ true
31
+ end
32
+
33
+ def destroy?
34
+ true
35
+ end
36
+
37
+ def destroy_all?
38
+ true
39
+ end
40
+
41
+
42
+ def scope
43
+ Pundit.policy_scope!(user, record.class)
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ class CategoryPolicy < ApplicationPolicy
2
+ class Scope < Struct.new(:user, :scope)
3
+ def resolve
4
+ scope
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ class PostPolicy < ApplicationPolicy
2
+ class Scope < Struct.new(:user, :scope)
3
+ def resolve
4
+ scope
5
+ end
6
+ end
7
+
8
+ def update?
9
+ record.author == user
10
+ end
11
+
12
+ def destroy?
13
+ update?
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class StorePolicy < ApplicationPolicy
2
+ class Scope < Struct.new(:user, :scope)
3
+ def resolve
4
+ scope
5
+ end
6
+ end
7
+
8
+ def destroy?
9
+ false
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class UserPolicy < ApplicationPolicy
2
+ class Scope < Struct.new(:user, :scope)
3
+ def resolve
4
+ scope
5
+ end
6
+ end
7
+
8
+ def destroy_all?
9
+ true
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'draper'
2
+
3
+ class PostDecorator < Draper::Decorator
4
+ decorates :post
5
+ delegate_all
6
+
7
+ def decorator_method
8
+ 'A method only available on the decorator'
9
+ end
10
+ end
11
+