Class: Wco::TagsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wco/tags_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_toObject



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/wco/tags_controller.rb', line 65

def add_to
  @tag = Wco::Tag.find params[:id]
  resource = params[:resource].constantize.find params[:resource_id]
  authorize! :update, @tag

  resource.tags.push @tag
  flag = resource.save
  flash_notice 'maybe?'
  redirect_to request.referrer
end

#add_to_manyObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/wco/tags_controller.rb', line 76

def add_to_many
  @tag = Wco::Tag.find params[:id]
  resources = params[:resource].constantize.find params[:resource_ids]
  authorize! :update, @tag

  flags = []
  resources.each do |resource|
    resource.tags.push @tag
    flags.push resource.save
  end
  flash_notice flags
  # redirect_to request.referrer
end

#createObject



6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/wco/tags_controller.rb', line 6

def create
  @tag = Wco::Tag.new params[:tag].permit!
  authorize! :create, @tag
  if @tag.save
    flash_notice "created tag"
  else
    flash_alert "Cannot create tag: #{@tag.errors.messages}"
  end
  redirect_to action: 'index'
end

#create_for_sidebarObject



56
57
58
59
60
61
62
63
# File 'app/controllers/wco/tags_controller.rb', line 56

def create_for_sidebar
  authorize! :create, Wco::Tag
  @current_profile.sidebar_tags.push Wco::Tag.find(params[:tag_id])
  @current_profile.save

  flash_notice 'Ok'
  redirect_to request.referrer
end

#destroyObject



17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/wco/tags_controller.rb', line 17

def destroy
  @tag = Wco::Tag.find params[:id]
  authorize! :destroy, @tag
  if @tag.destroy
    flash_notice 'ok'
  else
    flash_alert 'No luck.'
  end
  redirect_to request.referrer
end

#editObject



28
29
30
31
# File 'app/controllers/wco/tags_controller.rb', line 28

def edit
  @tag = Wco::Tag.find params[:id]
  authorize! :edit, @tag
end

#indexObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/wco/tags_controller.rb', line 33

def index
  authorize! :index, Wco::Tag
  @tags = Wco::Tag.all.order_by( slug: :asc )

  tags = Wco::Tag.all.to_a.group_by(&:parent_id)
  build_tree = lambda do |parent_id|
    (tags[parent_id] || []).sort_by { |tag| tag.slug.downcase }.map do |tag|
      { tag: tag, sons: build_tree.call(tag.id) }
    end
  end
  @tree = build_tree.call(nil)


  @template = params[:template] || 'index_tree'
end

#newObject



49
50
51
# File 'app/controllers/wco/tags_controller.rb', line 49

def new
  authorize! :new, Wco::Tag
end

#new_for_sidebarObject



53
54
55
# File 'app/controllers/wco/tags_controller.rb', line 53

def new_for_sidebar
  authorize! :new, Wco::Tag
end

#remove_fromObject



90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/wco/tags_controller.rb', line 90

def remove_from
  @tag = Wco::Tag.find params[:id]
  resource = params[:resource].constantize.find params[:resource_id]
  authorize! :update, @tag

  resource.tags.delete @tag
  flag = resource.save
  flash_notice 'maybe?'
  redirect_to request.referrer
end

#remove_from_manyObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/wco/tags_controller.rb', line 101

def remove_from_many
  @tag = Wco::Tag.find params[:id]
  resources = params[:resource].constantize.find params[:resource_ids]
  authorize! :update, @tag

  flags = []
  resources.each do |resource|
    resource.tags.delete @tag
    flags.push resource.save
  end
  flash_notice flags
  # redirect_to request.referrer
end

#showObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/wco/tags_controller.rb', line 115

def show
  @tag = Wco::Tag.find params[:id]
  authorize! :show, @tag

  @galleries = @tag.galleries.page( params[:galleries_page] ).per( current_profile.per_page )
  @leads     = @tag.leads.page( params[::Wco::Lead::PAGE_PARAM_NAME] ).per( current_profile.per_page )
  @leadsets  = @tag.leadsets.page( params[::Wco::Leadset::PAGE_PARAM_NAME] ).per( current_profile.per_page )
  @reports   = @tag.reports.page( params[:reports_page] ).per( current_profile.per_page )

  @conversations, @messages, _ = WcoEmail::Conversation.load_conversations_messages_tag_by_params_and_profile( {tagname: @tag.slug}, current_profile )

  # render params['template'] || 'show'
end

#updateObject



129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/wco/tags_controller.rb', line 129

def update
  @tag = Wco::Tag.find params[:id]
  authorize! :update, @tag
  if @tag.update params[:tag].permit!
    flash_notice "updated tag"
  else
    flash_alert "Cannot update tag: #{@tag.errors.messages}"
  end
  redirect_to action: 'index'
end