Module: Iord::Nested

Extended by:
ActiveSupport::Concern
Defined in:
lib/iord/nested.rb

Instance Method Summary collapse

Instance Method Details

#build_resource_info_with_nestedObject



98
99
100
101
102
103
104
# File 'lib/iord/nested.rb', line 98

def build_resource_info_with_nested
  return if @parent_path
  build_resource_info_without_nested

  parent_path = self.parent_models.map { |x| x.name.underscore }
  @parent_path = parent_path.join('/')
end

#collection_url_with_nestedObject



54
55
56
# File 'lib/iord/nested.rb', line 54

def collection_url_with_nested
  self.public_send collection_url_method, *@parents
end

#create_resourceObject



38
39
40
41
42
43
44
45
46
# File 'lib/iord/nested.rb', line 38

def create_resource
  @resource = resource_class.new resource_params
  if @parent.respond_to? "#{parent_resource_name}=".to_sym
    @parent.public_send "#{parent_resource_name}=".to_sym, @resource
  elsif @parent.respond_to? parent_collection_name
    collection = @parent.public_send parent_collection_name
    collection << @resource
  end
end

#edit_resource_url_with_nested(resource = nil) ⇒ Object



58
59
60
61
62
# File 'lib/iord/nested.rb', line 58

def edit_resource_url_with_nested(resource = nil)
  resource ||= @resource
  resource = nil unless has_collection?
  self.public_send "edit_#{resource_url_method}".to_sym, *(@parents + [resource])
end

#index_collectionObject



34
35
36
# File 'lib/iord/nested.rb', line 34

def index_collection
  @collection = @parent.public_send(parent_collection_name)
end

#parent_collection_nameObject



24
25
26
27
# File 'lib/iord/nested.rb', line 24

def parent_collection_name
  @parent_collection_name ||=
    default(:parent_collection_name) || resource_name_u.pluralize.to_sym
end

#parent_modelsObject



16
17
18
# File 'lib/iord/nested.rb', line 16

def parent_models
  @parent_models ||= default(:parent_models) || []
end

#parent_resource_nameObject



29
30
31
32
# File 'lib/iord/nested.rb', line 29

def parent_resource_name
  @parent_resource_name ||=
    default(:parent_resource_name) || resource_name_u.to_sym
end

#parents_collection_namesObject



20
21
22
# File 'lib/iord/nested.rb', line 20

def parents_collection_names
  @parents_collection_names ||= default(:parents_collection_names) || []
end

#resource_url_with_nested(resource = nil) ⇒ Object



48
49
50
51
52
# File 'lib/iord/nested.rb', line 48

def resource_url_with_nested(resource = nil)
  resource ||= @resource
  resource = nil unless has_collection?
  self.public_send resource_url_method, *(@parents + [resource])
end

#set_resource_with_nestedObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/iord/nested.rb', line 64

def set_resource_with_nested
  @parents = Array.new
  @parent = nil
  if self.parent_models.empty?
    path = request.path[1..-1].split('/').reject { |x| x =~ /^#{params[:action]}|[a-f0-9]+(\.#{params[:format]})?$/ }
    path = path[0..-2].map { |x| x.singularize.camelize.constantize }
    @parent_models = path
  end
  self.parent_models.each_with_index do |model, index|
    param = "#{model.name.underscore}_id".to_sym
    collection = model
    unless @parents.empty?
      collection = @parent
      collection_name = self.parents_collection_names[index]
      if collection_name.nil?
        collection_name = model.name.underscore.pluralize.to_sym
      end
      collection = @parent.public_send(collection_name)
    end
    @parent = collection.find(params[param])
    @parents << @parent
  end
  return unless self.class.resource_based_actions.include? params[:action].to_sym
  if @parent.respond_to? parent_collection_name
    @resource = @parent.public_send(parent_collection_name).find(params[:id])
  else
    @resource = @parent.public_send(parent_resource_name)
    if @resource.nil?
      @resource = resource_class.new
      @parent.public_send("#{parent_resource_name}=".to_sym, @resource)
    end
  end
end