Class: Admin::TaxonsController

Inherits:
BaseController
  • Object
show all
Includes:
Railslove::Plugins::FindByParam::SingletonMethods
Defined in:
app/controllers/admin/taxons_controller.rb

Instance Method Summary collapse

Instance Method Details

#availableObject



110
111
112
113
114
115
116
# File 'app/controllers/admin/taxons_controller.rb', line 110

def available
  @product = load_product
  @taxons = params[:q].blank? ? [] : Taxon.where('lower(name) LIKE ?', "%#{params[:q].mb_chars.downcase}%")
  @taxons.delete_if { |taxon| @product.taxons.include?(taxon) }

  respond_with(:admin, @taxons)
end

#batch_selectObject



138
139
140
141
142
143
144
# File 'app/controllers/admin/taxons_controller.rb', line 138

def batch_select
  @product = load_product
  @taxons = params[:taxon_ids].map{|id| Taxon.find(id)}.compact
  @product.taxons = @taxons
  @product.save
  redirect_to selected_admin_product_taxons_url(@product)
end

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/admin/taxons_controller.rb', line 6

def create
  @taxonomy = Taxonomy.find(params[:taxonomy_id])
  @taxon = @taxonomy.taxons.build(params[:taxon])
  if @taxon.save
    respond_with(@taxon) do |format|
      format.json {render :json => @taxon.to_json }
    end
  else
    flash[:error] = I18n.t('errors.messages.could_not_create_taxon')
    respond_with(@taxon) do |format|
      format.html { redirect_to @taxonomy ? edit_admin_taxonomy_url(@taxonomy) : admin_taxonomies_url }
    end
  end
end

#destroyObject



97
98
99
100
101
# File 'app/controllers/admin/taxons_controller.rb', line 97

def destroy
  @taxon = Taxon.find(params[:id])
  @taxon.destroy
  respond_with(@taxon) { |format| format.json { render :json => '' } }
end

#editObject



21
22
23
24
25
26
27
# File 'app/controllers/admin/taxons_controller.rb', line 21

def edit
  @taxonomy = Taxonomy.find(params[:taxonomy_id])
  @taxon = @taxonomy.taxons.find(params[:id])
  @permalink_part = @taxon.permalink.split("/").last

  respond_with(:admin, @taxon) 
end

#removeObject



118
119
120
121
122
123
124
125
126
# File 'app/controllers/admin/taxons_controller.rb', line 118

def remove
  @product = load_product
  @taxon = Taxon.find(params[:id])
  @product.taxons.delete(@taxon)
  @product.save
  @taxons = @product.taxons

  respond_with(@taxon) { |format| format.js { render_js_for_destroy } }
end

#selectObject



128
129
130
131
132
133
134
135
136
# File 'app/controllers/admin/taxons_controller.rb', line 128

def select
  @product = load_product
  @taxon = Taxon.find(params[:id])
  @product.taxons << @taxon
  @product.save
  @taxons = @product.taxons

  respond_with(:admin, @taxons)
end

#selectedObject



103
104
105
106
107
108
# File 'app/controllers/admin/taxons_controller.rb', line 103

def selected
  @product = load_product
  @taxons = @product.taxons

  respond_with(:admin, @taxons)
end

#updateObject



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
92
93
94
95
# File 'app/controllers/admin/taxons_controller.rb', line 29

def update
  @taxonomy = Taxonomy.find(params[:taxonomy_id])
  @taxon = @taxonomy.taxons.find(params[:id])
  parent_id = params[:taxon][:parent_id]
  new_position = params[:taxon][:position]

  if parent_id || new_position #taxon is being moved
    new_parent = parent_id.nil? ? @taxon.parent : Taxon.find(parent_id.to_i)
    new_position = new_position.nil? ? -1 : new_position.to_i

    # Bellow is a very complicated way of finding where in nested set we
    # should actually move the taxon to achieve sane results,
    # JS is giving us the desired position, which was awesome for previous setup,
    # but now it's quite complicated to find where we should put it as we have
    # to differenciate between moving to the same branch, up down and into
    # first position.
    new_siblings = new_parent.children
    if new_position <= 0 && new_siblings.empty?
      @taxon.move_to_child_of(new_parent)
    elsif new_parent.id != @taxon.parent_id
      if new_position == 0
        @taxon.move_to_left_of(new_siblings.first)
      else
        @taxon.move_to_right_of(new_siblings[new_position-1])
      end
    elsif new_position < new_siblings.index(@taxon)
      @taxon.move_to_left_of(new_siblings[new_position]) # we move up
    else
      @taxon.move_to_right_of(new_siblings[new_position]) # we move down
    end
    # Reset legacy position, if any extensions still rely on it
    new_parent.children.reload.each{|t| t.update_attribute(:position, t.position)}
    
    if parent_id
      @taxon.reload
      @taxon.set_permalink
      @taxon.save!
      @update_children = true
    end
  end

  if params.key? "permalink_part"
    parent_permalink = @taxon.permalink.split("/")[0...-1].join("/")
    parent_permalink += "/" unless parent_permalink.blank?
    params[:taxon][:permalink] = parent_permalink + params[:permalink_part]
  end
  #check if we need to rename child taxons if parent name or permalink changes
  @update_children = true if params[:taxon][:name] != @taxon.name || params[:taxon][:permalink] != @taxon.permalink

  if @taxon.update_attributes(params[:taxon])
    flash[:notice] = flash_message_for(@taxon, :successfully_updated)
  end

  #rename child taxons
  if @update_children
    @taxon.descendants.each do |taxon|
      taxon.reload
      taxon.set_permalink
      taxon.save!
    end
  end
  
  respond_with(@taxon) do |format|
    format.html {redirect_to edit_admin_taxonomy_url(@taxonomy) }
    format.json {render :json => @taxon.to_json }
  end
end