Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get selects to work with ActiveRecord associated records (belongs_to) #530

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions lib/best_in_place/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def best_in_place(object, field, opts = {})
options = {}
options[:data] = HashWithIndifferentAccess.new(opts[:data])
options[:data]['bip-type'] = type
options[:data]['bip-attribute'] = field

real_object = best_in_place_real_object_for object

Expand All @@ -19,14 +18,35 @@ def best_in_place(object, field, opts = {})

if opts[:collection] or type == :checkbox
collection = opts[:collection]
value = value.to_s
collection = best_in_place_default_collection if collection.blank?
collection = best_in_place_collection_builder(type, collection)
display_value = collection.flat_map{|a| a[0].to_s == value ? a[1] : nil }.compact[0]
collection = collection.to_json
options[:data]['bip-collection'] = html_escape(collection)
if (value.is_a? ActiveRecord::Base) && (collection.is_a? ActiveRecord::Relation) && (collection.model == value.class)
bipCollection = []
indexField = opts[:indexField] || :id
collection.each do |collection_item|
item_index = collection_item[indexField]
if opts[:valueField]
item_display_value = collection_item[opts[:valueField]]
else
item_display_value = collection_item.to_s
end
bipCollection << [item_index,item_display_value];
if value == collection_item
display_value = item_display_value
end
end
field = field + '_' + indexField.to_s
else
value = value.to_s
bipCollection = best_in_place_collection_builder(type, collection)
display_value = bipCollection.flat_map{|a| a[0].to_s == value ? a[1] : nil }.compact[0]
end
bipCollectionAsJson = bipCollection.to_json
options[:data]['bip-collection'] = html_escape(bipCollectionAsJson)
end

# this must come after the collection block in case the field name gets updated with _id
options[:data]['bip-attribute'] = field

options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)

Expand Down