dynamic_fields_for_rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +1 -0
  2. data/README.md +52 -10
  3. data/Rakefile +14 -1
  4. data/app/helpers/dynamic_fields_for_helper.rb +4 -6
  5. data/dynamic_fields_for_rails.gemspec +2 -0
  6. data/lib/dynamic_fields_for_rails.rb +11 -1
  7. data/lib/dynamic_fields_for_rails/version.rb +1 -1
  8. data/test/config/active_record.rb +9 -0
  9. data/test/dummy/Rakefile +7 -0
  10. data/test/dummy/app/controllers/application_controller.rb +3 -0
  11. data/test/dummy/app/helpers/application_helper.rb +2 -0
  12. data/test/dummy/app/models/child.rb +2 -0
  13. data/test/dummy/app/models/parent.rb +4 -0
  14. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  15. data/test/dummy/app/views/parents/_child_fields.html.erb +0 -0
  16. data/test/dummy/app/views/parents/form.html.erb +1 -0
  17. data/test/dummy/config.ru +4 -0
  18. data/test/dummy/config/application.rb +41 -0
  19. data/test/dummy/config/boot.rb +10 -0
  20. data/test/dummy/config/database.yml +22 -0
  21. data/test/dummy/config/environment.rb +5 -0
  22. data/test/dummy/config/environments/development.rb +26 -0
  23. data/test/dummy/config/environments/production.rb +49 -0
  24. data/test/dummy/config/environments/test.rb +35 -0
  25. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  26. data/test/dummy/config/initializers/inflections.rb +10 -0
  27. data/test/dummy/config/initializers/mime_types.rb +5 -0
  28. data/test/dummy/config/initializers/secret_token.rb +7 -0
  29. data/test/dummy/config/initializers/session_store.rb +8 -0
  30. data/test/dummy/config/locales/en.yml +5 -0
  31. data/test/dummy/config/routes.rb +59 -0
  32. data/test/dummy/db/development.sqlite3 +0 -0
  33. data/test/dummy/db/migrate/20130423140255_create_parents.rb +8 -0
  34. data/test/dummy/db/migrate/20130423140307_create_children.rb +8 -0
  35. data/test/dummy/db/schema.rb +27 -0
  36. data/test/dummy/db/test.sqlite3 +0 -0
  37. data/test/dummy/log/development.log +8 -0
  38. data/test/dummy/log/production.log +0 -0
  39. data/test/dummy/log/server.log +0 -0
  40. data/test/dummy/log/test.log +1151 -0
  41. data/test/dummy/public/404.html +26 -0
  42. data/test/dummy/public/422.html +26 -0
  43. data/test/dummy/public/500.html +26 -0
  44. data/test/dummy/public/favicon.ico +0 -0
  45. data/test/dummy/public/javascripts/application.js +2 -0
  46. data/test/dummy/public/javascripts/controls.js +965 -0
  47. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  48. data/test/dummy/public/javascripts/effects.js +1123 -0
  49. data/test/dummy/public/javascripts/prototype.js +6001 -0
  50. data/test/dummy/public/javascripts/rails.js +202 -0
  51. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  52. data/test/dummy/script/rails +6 -0
  53. data/test/dynamic_fields_for_helper_test.rb +7 -0
  54. data/test/link_to_add_fields_test.rb +34 -0
  55. data/test/link_to_delete_fields_test.rb +40 -0
  56. data/test/test_helper.rb +17 -0
  57. metadata +133 -3
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ .DS_Store
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/README.md CHANGED
@@ -6,15 +6,63 @@ Dynamic fields helper for Rails.
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'dynamic_fields_for_rails'
9
+ ```
10
+ gem 'dynamic_fields_for_rails'
11
+ ```
10
12
 
11
13
  And then execute:
12
14
 
13
- $ bundle
15
+ ```
16
+ $ bundle
17
+ ```
14
18
 
15
19
  Or install it yourself as:
16
20
 
17
- $ gem install dynamic_fields_for_rails
21
+ ```
22
+ $ gem install dynamic_fields_for_rails
23
+ ```
24
+
25
+ Add this to your application.coffee
26
+
27
+ ```
28
+ #= require dynamic_fields_for
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Create a partial for your nested resource called "_{nested_resoure}_fields.haml" or with .html and so on.
34
+ Add to the partial only the fields you want to edit like this:
35
+
36
+ ```
37
+ = fields.label :field1
38
+ = fields.text_field :field1
39
+
40
+ = fields.label :field2
41
+ = fields.text_field :field2
42
+
43
+ = link_to_delete_fields fields, "Delete"
44
+ ```
45
+
46
+ The "link_to_delete_fields" helper is a easy way to make the nested fields deletable. You can put whatever divs/fieldsets etc. around those fields to style them.
47
+
48
+ Put this in your Form and replace the {nested_resource} and {resource} tags with the name of your resources:
49
+
50
+ ```
51
+ = form.fields_for :{nested_resource}s, {resource}.{nested_resource}s do |builder|
52
+ = render '{nested_resource}_fields', fields: builder
53
+ = link_to_add_fields form, :{nested_resource}s, "Add Entry"
54
+ ```
55
+
56
+ To add custom css classes to the add and delete buttons just add an initializer called "dynamic_fields_for_rails.rb" and paste in the following and add your custom classes:
57
+
58
+ ```
59
+ # encoding: utf-8
60
+
61
+ DynamicFieldsForRails.setup do |config|
62
+ config.delete_css_classes = ""
63
+ config.add_css_classes = ""
64
+ end
65
+ ```
18
66
 
19
67
  ## Contributing
20
68
 
@@ -22,10 +70,4 @@ Or install it yourself as:
22
70
  2. Create your feature branch (`git checkout -b my-new-feature`)
23
71
  3. Commit your changes (`git commit -am 'Add some feature'`)
24
72
  4. Push to the branch (`git push origin my-new-feature`)
25
- 5. Create new Pull Request
26
-
27
- ## TODOs
28
-
29
-
30
- - Write usage instructions here
31
- - Write Tests
73
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,14 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/setup'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+
14
+ task :default => :test
@@ -5,9 +5,9 @@ module DynamicFieldsForHelper
5
5
  new_object = form.object.send(association).klass.new
6
6
  id = new_object.object_id
7
7
  fields = form.fields_for(association, new_object, child_index: id) do |builder|
8
- render(association.to_s.singularize + "_fields", fields: builder)
8
+ render("#{form.object.class.name.downcase.pluralize}/#{association.to_s.singularize}_fields", fields: builder)
9
9
  end
10
- style_class = "button small radius right"
10
+ style_class = DynamicFieldsForRails.add_css_classes
11
11
  if block_given?
12
12
  link_to('#', class: "add_fields #{style_class}", data: {id: id, fields: fields.gsub("\n", "")}, &block)
13
13
  else
@@ -15,12 +15,10 @@ module DynamicFieldsForHelper
15
15
  end
16
16
  end
17
17
 
18
- def link_to_delete_fields(fields, name=nil, style_class=nil, &block)
19
- if style_class.blank?
20
- style_class = "button alert radius postfix"
21
- end
18
+ def link_to_delete_fields(fields, name=nil, &block)
22
19
  link = []
23
20
  link << fields.hidden_field(:_destroy)
21
+ style_class = DynamicFieldsForRails.delete_css_classes
24
22
  if block_given?
25
23
  link << link_to('#', class: "remove_fields #{style_class}", title: name, &block)
26
24
  else
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha"
24
+ spec.add_development_dependency "sqlite3"
23
25
 
24
26
  spec.add_dependency "rails", ">= 3.2.13"
25
27
  end
@@ -1,6 +1,16 @@
1
1
  require "dynamic_fields_for_rails/version"
2
2
 
3
3
  module DynamicFieldsForRails
4
- class Engine < ::Rails::Engine
4
+ class Engine < Rails::Engine
5
+ end
6
+
7
+ mattr_accessor :add_css_classes
8
+ @@add_css_classes = ""
9
+
10
+ mattr_accessor :delete_css_classes
11
+ @@delete_css_classes = ""
12
+
13
+ def self.setup
14
+ yield self
5
15
  end
6
16
  end
@@ -1,3 +1,3 @@
1
1
  module DynamicFieldsForRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,9 @@
1
+ class ActiveRecord::Base
2
+ mattr_accessor :shared_connection
3
+ @@shared_connection = nil
4
+
5
+ def self.connection
6
+ @@shared_connection || retrieve_connection
7
+ end
8
+ end
9
+ ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
@@ -0,0 +1,7 @@
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 File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Child < ActiveRecord::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class Parent < ActiveRecord::Base
2
+ has_many :children
3
+ accepts_nested_attributes_for :children, allow_destroy: true
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1 @@
1
+ form.html.erb
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "rails/all"
4
+
5
+ Bundler.require
6
+ require "dynamic_fields_for_rails"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # JavaScript files you want as :defaults (application.js is always included).
33
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
34
+
35
+ # Configure the default encoding used in templates for Ruby 1.9.
36
+ config.encoding = "utf-8"
37
+
38
+ # Configure sensitive parameters which will be filtered from the log file.
39
+ config.filter_parameters += [:password]
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,35 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!