Skip to content

Commit f3d34bd

Browse files
committed
basic rating posting working
1 parent 87311c9 commit f3d34bd

File tree

11 files changed

+120
-39
lines changed

11 files changed

+120
-39
lines changed

app/controllers/ratings_controller.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,16 @@ def index
3838
def create
3939
rating = PostCreator.new(current_user, rating_params).create
4040
if rating.persisted?
41-
# We could notify the user here
42-
serializer = UserFeedback::RatingSerializer
43-
render json: serializer.new(rating, scope: self, root: false).as_json , status: :created
41+
action = UserAction.where(target_post_id: rating.id, action_type: UserAction::REPLY).first
42+
render_serialized(UserAction.ratings(action_id: action.id).first, UserFeedback::RatingSerializer)
4443
else
4544
render json: { errors: 'Unable to create or update post' }, status: :unprocessable_entity
4645
end
4746
end
4847

4948
private
5049

51-
# Create a first post, if not the first rating will be omitted
50+
# Create a first post, otherwise the first rating will be omitted
5251
def ensure_topic_existence
5352
@user = User.find_by(id: params[:user_id])
5453
raise Discourse::NotFound if @user.blank?

app/models/user_action.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def ratings(opts)
77
offset = opts[:offset] || 0
88
limit = opts[:limit] || 60
99

10+
action_id = opts[:action_id]
11+
1012
# The weird thing is that target_post_id can be null, so it makes everything
1113
# ever so more complex. Should we allow this, not sure.
1214
builder = SqlBuilder.new <<-SQL
@@ -42,12 +44,16 @@ def ratings(opts)
4244
/*limit*/
4345
SQL
4446

45-
builder.where("a.action_type in (:action_types)", action_types: action_types) if action_types && action_types.length > 0
46-
builder.where("a.target_topic_id = :target_topic_id", target_topic_id: target_topic_id)
47-
builder
48-
.order_by("a.created_at desc")
49-
.offset(offset.to_i)
50-
.limit(limit.to_i)
47+
if action_id
48+
builder.where("a.id = :id", id: action_id.to_i)
49+
else
50+
builder.where("a.action_type in (:action_types)", action_types: action_types) if action_types && action_types.length > 0
51+
builder.where("a.target_topic_id = :target_topic_id", target_topic_id: target_topic_id)
52+
builder
53+
.order_by("a.created_at desc")
54+
.offset(offset.to_i)
55+
.limit(limit.to_i)
56+
end
5157

5258
builder.map_exec(UserAction::UserActionRow)
5359
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { emojiUnescape } from 'discourse/lib/text';
2+
import UserAction from 'discourse/models/user-action';
3+
import { ajax } from 'discourse/lib/ajax'
4+
5+
export default Ember.Component.extend({
6+
raw: '',
7+
rating: 0,
8+
actions: {
9+
updateRating(rating){
10+
this.set('rating', rating);
11+
},
12+
save(){
13+
const self = this;
14+
this.set('raw', this.$('textarea').val());
15+
this.set('loading', true);
16+
const stream = this.get('stream');
17+
const user = stream.get('user');
18+
19+
ajax(`/user-feedback/users/${user.id}.json`, {
20+
type: 'POST',
21+
data: {
22+
raw: self.get('raw'),
23+
rating: self.get('rating')
24+
}
25+
}).then(function (data) {
26+
let action = data.rating;
27+
const copy = Em.A();
28+
copy.pushObject(action);
29+
stream.get('content').insertAt(0, UserAction.collapseStream(copy)[0]);
30+
stream.set('itemsLoaded', stream.get('itemsLoaded') + 1)
31+
}).finally(function(){
32+
self.set('loading', false);
33+
});
34+
}
35+
},
36+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export default Ember.Component.extend({
2+
starsNumber: 5,
3+
readOnly: true,
4+
rating: 0,
5+
didReceiveAttrs(){
6+
this._super(...arguments);
7+
const stars = [];
8+
for(let i=0; i<this.get('starsNumber'); i++){
9+
let star = { full: i < this.get('rating') };
10+
stars.push(star);
11+
}
12+
this.set('stars', stars);
13+
},
14+
didInsertElement(){
15+
this._super(...arguments);
16+
if(this.get('readOnly') === false){
17+
this.$().css('cursor', 'pointer');
18+
}
19+
this.$().css('display', 'inline-block');
20+
},
21+
click(event){
22+
let rating = this.getTarget(event.pageX);
23+
let stars = this.$('.star-icon');
24+
stars.each(function(index, element){
25+
if(index < rating){
26+
$(element).addClass('full');
27+
}else{
28+
$(element).removeClass('full');
29+
}
30+
});
31+
if(this.get('action') !== undefined){
32+
this.get('action')(rating);
33+
}
34+
},
35+
getTarget(x){
36+
const starsNumber = this.get('starsNumber');
37+
return Math.ceil((starsNumber * (x - this.$().offset().left) / this.$().width()));
38+
}
39+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="rating-composer">
2+
{{rating-stars rating=0 readOnly=false action=(action "updateRating")}}
3+
<textarea class="rating-textarea" placeholder="Describe your rating here" rows="4"></textarea>
4+
<button {{action "save"}} tabindex="5" class="btn btn-primary create {{if disableSubmit 'disabled'}}" title="{{i18n 'composer.title'}}"><i class="fa fa-pencil"></i>{{i18n 'composer.save_edit'}}</button>
5+
{{#if loading}}
6+
{{loading-spinner size="small"}}
7+
{{/if}}
8+
</div>

assets/javascripts/discourse/templates/components/rating-item.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<span class="title">
66
<a href={{item.userUrl}}>{{#if item.acting_name}}{{item.acting_name}}{{else}}{{item.acting_username}}{{/if}}</a>
77
</span>
8-
<div class="category">{{mount-widget widget="rating" args=item.rating}}</div>
8+
<div class="category">{{rating-stars rating=item.rating readOnly=true}}</div>
99
{{plugin-outlet name="user-stream-item-header" args=(hash item=item)}}
1010
</div>
1111

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="rating-stars">
2+
{{#each stars as |star|}}
3+
<span class="star-icon {{if star.full 'full' ''}}">☆</span>
4+
{{/each}}
5+
</div>

assets/javascripts/discourse/templates/user-activity-feedback.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{{model.noContentHelp}}}
44
</div>
55
{{/if}}
6-
{{mount-widget widget="rating-composer"}}
6+
{{rating-composer stream=model}}
77
{{#user-stream stream=model}}
88
{{#each model.content as |item|}}
99
{{rating-item item=item removeBookmark="removeBookmark"}}

assets/javascripts/discourse/widgets/rating-composer.js.es6

Lines changed: 0 additions & 7 deletions
This file was deleted.

assets/javascripts/discourse/widgets/rating.js.es6

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)