activeadmin-magicfields 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/Gemfile +42 -0
  4. data/Guardfile +8 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +160 -0
  7. data/Rakefile +33 -0
  8. data/activeadmin-magicfields.gemspec +23 -0
  9. data/app/assets/javascripts/activeadmin-magicfields.js.coffee +101 -0
  10. data/app/assets/stylesheets/activeadmin-magicfields.css.scss +53 -0
  11. data/lib/activeadmin-magicfields.rb +18 -0
  12. data/lib/activeadmin-magicfields/engine.rb +12 -0
  13. data/lib/activeadmin-magicfields/form_builder.rb +175 -0
  14. data/lib/activeadmin-magicfields/paperclip.rb +2 -0
  15. data/lib/activeadmin-magicfields/version.rb +3 -0
  16. data/lib/app/admin/part.rb +29 -0
  17. data/lib/app/admin/part_object.rb +26 -0
  18. data/lib/app/models/field/checkbox.rb +12 -0
  19. data/lib/app/models/field/image.rb +35 -0
  20. data/lib/app/models/field/repeater.rb +19 -0
  21. data/lib/app/models/field/text.rb +14 -0
  22. data/lib/app/models/field/textarea.rb +14 -0
  23. data/lib/app/models/field_template.rb +5 -0
  24. data/lib/app/models/part.rb +6 -0
  25. data/lib/app/models/part_object.rb +11 -0
  26. data/lib/app/models/part_object_field.rb +13 -0
  27. data/lib/app/models/repeater_part_object.rb +6 -0
  28. data/lib/app/views/admin/field_inputs/_checkbox.html.erb +1 -0
  29. data/lib/app/views/admin/field_inputs/_image.html.erb +4 -0
  30. data/lib/app/views/admin/field_inputs/_product.html.erb +1 -0
  31. data/lib/app/views/admin/field_inputs/_repeater.html.erb +3 -0
  32. data/lib/app/views/admin/field_inputs/_site_page.html.erb +1 -0
  33. data/lib/app/views/admin/field_inputs/_text.html.erb +1 -0
  34. data/lib/app/views/admin/field_inputs/_textarea.html.erb +8 -0
  35. data/lib/app/views/admin/part_objects/_part_object.html.erb +53 -0
  36. data/lib/app/views/admin/parts/_part.html.erb +11 -0
  37. data/lib/app/views/admin/parts/_part_old.arb +32 -0
  38. data/lib/generators/activeadmin-magicfields/install/install_generator.rb +33 -0
  39. data/lib/generators/activeadmin-magicfields/install/templates/create_field_checkbox.rb +10 -0
  40. data/lib/generators/activeadmin-magicfields/install/templates/create_field_image.rb +10 -0
  41. data/lib/generators/activeadmin-magicfields/install/templates/create_field_repeater.rb +9 -0
  42. data/lib/generators/activeadmin-magicfields/install/templates/create_field_templates.rb +14 -0
  43. data/lib/generators/activeadmin-magicfields/install/templates/create_field_text.rb +10 -0
  44. data/lib/generators/activeadmin-magicfields/install/templates/create_field_textarea.rb +12 -0
  45. data/lib/generators/activeadmin-magicfields/install/templates/create_part_object_fields.rb +10 -0
  46. data/lib/generators/activeadmin-magicfields/install/templates/create_part_objects.rb +13 -0
  47. data/lib/generators/activeadmin-magicfields/install/templates/create_parts.rb +9 -0
  48. data/lib/generators/activeadmin-magicfields/install/templates/create_repeater_part_objects.rb +10 -0
  49. data/spec/rails_helper.rb +154 -0
  50. data/spec/spec_helper.rb +17 -0
  51. data/spec/support/deferred_garbage_collection.rb +19 -0
  52. data/spec/support/detect_rails_version.rb +26 -0
  53. data/spec/support/integration_example_group.rb +31 -0
  54. data/spec/support/jslint.yml +80 -0
  55. data/spec/support/rails_template.rb +104 -0
  56. data/spec/support/rails_template_with_data.rb +59 -0
  57. data/spec/support/templates/cucumber.rb +24 -0
  58. data/spec/support/templates/cucumber_with_reloading.rb +5 -0
  59. data/spec/support/templates/en.yml +8 -0
  60. data/spec/support/templates/policies/active_admin/comment_policy.rb +9 -0
  61. data/spec/support/templates/policies/active_admin/page_policy.rb +18 -0
  62. data/spec/support/templates/policies/application_policy.rb +45 -0
  63. data/spec/support/templates/policies/category_policy.rb +7 -0
  64. data/spec/support/templates/policies/post_policy.rb +15 -0
  65. data/spec/support/templates/policies/store_policy.rb +11 -0
  66. data/spec/support/templates/policies/user_policy.rb +11 -0
  67. data/spec/support/templates/post_decorator.rb +11 -0
  68. data/spec/unit/form_builder_spec.rb +121 -0
  69. data/tasks/test.rake +83 -0
  70. metadata +175 -0
@@ -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