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

Embedded Relations in Mongoid #498

Merged
merged 2 commits into from
Jul 12, 2015
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion lib/ransack/adapters/mongoid/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def ransackable_associations(auth_object = nil)
end

def reflect_on_all_associations_all
reflect_on_all_associations(:belongs_to, :has_one, :has_many)
reflect_on_all_associations(:belongs_to, :has_one, :has_many, :embeds_many, :embedded_in)
end

# For overriding with a whitelist of symbols
Expand All @@ -87,6 +87,10 @@ def joins_values *args
[]
end

def custom_join_ast *args
[]
end

def first(*args)
if args.size == 0
super
Expand Down
8 changes: 4 additions & 4 deletions lib/ransack/adapters/mongoid/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def relation_for(object)

def type_for(attr)
return nil unless attr && attr.valid?
name = attr.arel_attribute.name.to_s
name = attr.arel_attribute.name.to_s.split('.').last
# table = attr.arel_attribute.relation.table_name

# schema_cache = @engine.connection.schema_cache
Expand All @@ -38,7 +38,7 @@ def type_for(attr)

name = '_id' if name == 'id'

t = object.klass.fields[name].type
t = object.klass.fields[name].try(:type) || @bind_pairs[attr.name].first.fields[name].type

t.to_s.demodulize.underscore.to_sym
end
Expand Down Expand Up @@ -114,10 +114,10 @@ def get_parent_and_attribute_name(str, parent = @base)
segments.pop) && segments.size > 0 && !found_assoc do
assoc, klass = unpolymorphize_association(segments.join('_'))
if found_assoc = get_association(assoc, parent)
join = build_or_find_association(found_assoc.name, parent, klass)
parent, attr_name = get_parent_and_attribute_name(
remainder.join('_'), join
remainder.join('_'), found_assoc.klass
)
attr_name = "#{segments.join('_')}.#{attr_name}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/nodes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def name=(name)
def valid?
bound? && attr &&
context.klassify(parent).ransackable_attributes(context.auth_object)
.include?(attr_name)
.include?(attr_name.split('.').last)
end

def type
Expand Down