Class: FeatureBox::SuggestionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/feature_box/suggestions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability, #pages_helper, #retrieve_vars

Methods included from Helpers

define_helpers, included

Instance Method Details

#createObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/feature_box/suggestions_controller.rb', line 90

def create
  @suggestion = Suggestion.new
  authorize! :create, @suggestion
  if current_user.votes_left<=0 then 
    return redirect_to suggestions_listing_path, :flash => { 
      :error => 'You need at least one vote to add suggestions'
    }
  end
  @suggestion.title = params[:suggestion][:title]
  @suggestion.description = params[:suggestion][:description]
  @suggestion.category_id = params[:suggestion][:category_id]
  if current_user.admin?
    @suggestion.status = params[:suggestion][:status]
  end
  @suggestion.user = current_user
    if @suggestion.save
      redirect_to @suggestion, notice: 'Suggestion was successfully created.' 
    else
      render action: "new" 
    end
end

#destroyObject



126
127
128
129
# File 'app/controllers/feature_box/suggestions_controller.rb', line 126

def destroy
  @suggestion.destroy
  redirect_to suggestions_listing_url, notice: 'Suggestion was successfully deleted.'     
end

#editObject



87
88
# File 'app/controllers/feature_box/suggestions_controller.rb', line 87

def edit
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/feature_box/suggestions_controller.rb', line 5

def index
  authorize! :read, Suggestion

  redirect = get_and_validate_params
  if redirect != false
    return redirect
  end

  pages_helper do |limit, offset|
    @suggestions, @total = Suggestion.find_those_with(
      :category =>@active_category, 
      :order_type =>@active_order_type, 
      :limit => limit, 
      :offset => offset  
      )
  end

  @show_search=true
  @show_nav_bar=true
end

#my_commentsObject



43
44
45
# File 'app/controllers/feature_box/suggestions_controller.rb', line 43

def my_comments
  impact_helper :comments
end

#my_suggestionsObject



35
36
37
# File 'app/controllers/feature_box/suggestions_controller.rb', line 35

def my_suggestions
  impact_helper :suggestions
end

#my_votesObject



39
40
41
# File 'app/controllers/feature_box/suggestions_controller.rb', line 39

def my_votes
  impact_helper :votes
end

#newObject



79
80
81
82
83
84
85
# File 'app/controllers/feature_box/suggestions_controller.rb', line 79

def new
  if current_user.votes_left<=0 then 
    return redirect_to suggestions_listing_path, :flash => { 
      :error => 'You need at least one vote to add suggestions'
    }
  end
end

#searchObject



26
27
28
29
30
31
32
33
# File 'app/controllers/feature_box/suggestions_controller.rb', line 26

def search
  authorize! :read, Suggestion
  q="%"+params[:q].to_s+"%"
  limit=Settings.max_suggestions_on_page;
  @suggestions = Suggestion.where("title LIKE ? OR description LIKE ?",q,q).order("created_at DESC").limit(limit)
  @show_search=true
  render action: "index" 
end

#showObject



71
72
73
74
75
76
77
# File 'app/controllers/feature_box/suggestions_controller.rb', line 71

def show
  @active_category = @suggestion.category
  pages_helper do |limit, offset|
    @comments = @suggestion.comments.order("created_at DESC").limit(limit).offset(offset)
    @total = @suggestion.comments.order("created_at DESC").size
  end 
end

#updateObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/feature_box/suggestions_controller.rb', line 112

def update
  @suggestion.title = params[:suggestion][:title]
  @suggestion.description = params[:suggestion][:description]
  @suggestion.category_id = params[:suggestion][:category_id]
  if current_user.admin?
    @suggestion.status = params[:suggestion][:status]
  end
  if @suggestion.save
    redirect_to @suggestion, notice: 'Suggestion was successfully updated.' 
  else
    render action: "edit" 
  end
end

#voteObject

json method



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/feature_box/suggestions_controller.rb', line 48

def vote #json method
  @suggestion = Suggestion.find(params[:id])
  authorize! :create, Vote
  if !user_signed_in?
    {
      :error=>"Sign in to vote"
    }
  elsif can?(:read, @suggestion)
    @suggestion.vote current_user
    votes=@suggestion.user_votes current_user
    render :json => {
      :total_votes=>@suggestion.votes.size,
      :user_votes=>votes,
      :votes_left=>current_user.votes_left
    }
  else
    render :json => {
      :error=>"You are not allowed to vote"
    }
  end
   
end