dynamic_fields_for_rails 0.1.0 → 0.2.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.
- data/.gitignore +1 -0
- data/README.md +52 -10
- data/Rakefile +14 -1
- data/app/helpers/dynamic_fields_for_helper.rb +4 -6
- data/dynamic_fields_for_rails.gemspec +2 -0
- data/lib/dynamic_fields_for_rails.rb +11 -1
- data/lib/dynamic_fields_for_rails/version.rb +1 -1
- data/test/config/active_record.rb +9 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/child.rb +2 -0
- data/test/dummy/app/models/parent.rb +4 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/parents/_child_fields.html.erb +0 -0
- data/test/dummy/app/views/parents/form.html.erb +1 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +41 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +22 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +26 -0
- data/test/dummy/config/environments/production.rb +49 -0
- data/test/dummy/config/environments/test.rb +35 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +59 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20130423140255_create_parents.rb +8 -0
- data/test/dummy/db/migrate/20130423140307_create_children.rb +8 -0
- data/test/dummy/db/schema.rb +27 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +8 -0
- data/test/dummy/log/production.log +0 -0
- data/test/dummy/log/server.log +0 -0
- data/test/dummy/log/test.log +1151 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/javascripts/application.js +2 -0
- data/test/dummy/public/javascripts/controls.js +965 -0
- data/test/dummy/public/javascripts/dragdrop.js +974 -0
- data/test/dummy/public/javascripts/effects.js +1123 -0
- data/test/dummy/public/javascripts/prototype.js +6001 -0
- data/test/dummy/public/javascripts/rails.js +202 -0
- data/test/dummy/public/stylesheets/.gitkeep +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dynamic_fields_for_helper_test.rb +7 -0
- data/test/link_to_add_fields_test.rb +34 -0
- data/test/link_to_delete_fields_test.rb +40 -0
- data/test/test_helper.rb +17 -0
- metadata +133 -3
data/.gitignore
CHANGED
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
|
-
|
9
|
+
```
|
10
|
+
gem 'dynamic_fields_for_rails'
|
11
|
+
```
|
10
12
|
|
11
13
|
And then execute:
|
12
14
|
|
13
|
-
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
14
18
|
|
15
19
|
Or install it yourself as:
|
16
20
|
|
17
|
-
|
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
|
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
|
8
|
+
render("#{form.object.class.name.downcase.pluralize}/#{association.to_s.singularize}_fields", fields: builder)
|
9
9
|
end
|
10
|
-
style_class =
|
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,
|
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 <
|
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
|
data/test/dummy/Rakefile
ADDED
@@ -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
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
form.html.erb
|
@@ -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,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,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!
|