aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/referential_networks_controller.rb
blob: 3b76deba1a51c61db4bd1cb87330da090f8aec70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
class ReferentialNetworksController < ChouetteController
  include ReferentialSupport
  defaults :resource_class => Chouette::Network, :collection_name => 'networks', :instance_name => 'network'
  respond_to :html
  respond_to :xml
  respond_to :json
  respond_to :kml, :only => :show
  respond_to :js, :only => :index

  belongs_to :referential, :parent_class => Referential

  def show
    show! do
      @network = ReferentialNetworkDecorator.decorate(
        @network,
        context: {
          referential: referential
        }
      )
    end
  end

  def index
    index! do |format|
      format.html {
        if collection.out_of_bounds?
          redirect_to params.merge(:page => 1)
        end

        @networks = decorate_networks(@networks)
      }

      format.js {
        @networks = decorate_networks(@networks)
      }
    end
  end

  protected

  def build_resource
    super.tap do |network|
      network.line_referential = referential.line_referential
    end
  end

  def collection
    @q = referential.workbench.networks.search(params[:q])

    if sort_column && sort_direction
      @networks ||= @q.result(:distinct => true).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page])
    else
      @networks ||= @q.result(:distinct => true).order(:name).paginate(:page => params[:page])
    end
  end

  def collection_name
    'networks'
  end

  def resource_url(network = nil)
    referential_network_path(referential, network || resource)
  end

  def collection_url
    referential_networks_path(referential)
  end

  def network_params
    params.require(:network).permit(:objectid, :object_version, :version_date, :description, :name, :registration_number, :source_name, :source_type_name, :source_identifier, :comment )
  end

  private

  def sort_column
    referential.workbench.networks.column_names.include?(params[:sort]) ? params[:sort] : 'name'
  end
  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def decorate_networks(networks)
    ReferentialNetworkDecorator.decorate(
      networks,
      context: {
        referential: referential
      }
    )
  end

end