Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring stories #6

Merged
merged 9 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions TODO/spec/controllers/stories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
end

describe "GET /archive" do
let(:read_one) { StoryFactory.build(is_read: true) }
let(:read_two) { StoryFactory.build(is_read: true) }
let(:read_one) { StoryFactory.build(readed: true) }
let(:read_two) { StoryFactory.build(readed: true) }
let(:stories) { [read_one, read_two].paginate }
before { allow(StoryRepository).to receive(:read).and_return(stories) }

Expand All @@ -72,8 +72,8 @@
end

describe "GET /starred" do
let(:starred_one) { StoryFactory.build(is_starred: true) }
let(:starred_two) { StoryFactory.build(is_starred: true) }
let(:starred_one) { StoryFactory.build(starred: true) }
let(:starred_two) { StoryFactory.build(starred: true) }
let(:stories) { [starred_one, starred_two].paginate }
before { allow(StoryRepository).to receive(:starred).and_return(stories) }

Expand All @@ -88,24 +88,24 @@

describe "PUT /stories/:id" do
before { allow(StoryRepository).to receive(:fetch).and_return(story_one) }
context "is_read parameter" do
context "readed parameter" do
context "when it is not malformed" do
it "marks a story as read" do
expect(StoryRepository).to receive(:save).once

put "/stories/#{story_one.id}", { is_read: true }.to_json
put "/stories/#{story_one.id}", { readed: true }.to_json

expect(story_one.is_read).to eq true
expect(story_one.readed).to eq true
end
end

context "when it is malformed" do
it "marks a story as read" do
expect(StoryRepository).to receive(:save).once

put "/stories/#{story_one.id}", { is_read: "malformed" }.to_json
put "/stories/#{story_one.id}", { readed: "malformed" }.to_json

expect(story_one.is_read).to eq true
expect(story_one.readed).to eq true
end
end
end
Expand All @@ -128,20 +128,20 @@
end
end

context "is_starred parameter" do
context "starred parameter" do
context "when it is not malformed" do
it "marks a story as permanently starred" do
put "/stories/#{story_one.id}", { is_starred: true }.to_json
put "/stories/#{story_one.id}", { starred: true }.to_json

expect(story_one.is_starred).to eq true
expect(story_one.starred).to eq true
end
end

context "when it is malformed" do
it "marks a story as permanently starred" do
put "/stories/#{story_one.id}", { is_starred: "malformed" }.to_json
put "/stories/#{story_one.id}", { starred: "malformed" }.to_json

expect(story_one.is_starred).to eq true
expect(story_one.starred).to eq true
end
end
end
Expand Down Expand Up @@ -179,8 +179,8 @@
allow(FeedRepository).to receive(:fetch).and_return(story_one.feed)
allow(StoryRepository).to receive(:feed).and_return(stories)

story_one.is_read = false
story_two.is_read = true
story_one.readed = false
story_two.readed = true

get "/feed/#{story_one.feed.id}"

Expand Down
2 changes: 1 addition & 1 deletion TODO/spec/fever_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def make_request(extra_headers = {})
end

it "returns starred items when 'saved_item_ids' header is provided" do
expect(Story).to receive(:where).with(is_starred: true).and_return([story_one, story_two])
expect(Story).to receive(:where).with(starred: true).and_return([story_one, story_two])

make_request(saved_item_ids: nil)

Expand Down
12 changes: 6 additions & 6 deletions TODO/spec/javascript/spec/models/story_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ describe("Story", function(){
story.get("selected").should.be.true;
});

it("sets is_read to true if keep_unread isn't true", function(){
it("sets readed to true if keep_unread isn't true", function(){
var story = new Story();
(!! story.get("keep_unread")).should.be.false;
story.open();
story.get("is_read").should.be.true;
story.get("readed").should.be.true;
});

it("doesn't sets is_read to true if keep_unread is true", function(){
it("doesn't sets readed to true if keep_unread is true", function(){
var story = new Story();
story.set("keep_unread", true);
story.open();
(!! story.get("is_read")).should.be.false;
(!! story.get("readed")).should.be.false;
});

it("calls closeOthers, unselectAll, setSelection on collection", function(){
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("Story", function(){
it("returns false if it has changedAttributes but no id", function(){
var story = new Story();
sinon.stub(story, "changedAttributes", function(){
return {is_read: true};
return {readed: true};
});
story.shouldSave().should.be.false;
story.changedAttributes.should.have.been.calledOnce;
Expand All @@ -126,7 +126,7 @@ describe("Story", function(){
it("returns true if it has changedAttributes and an id", function(){
var story = new Story({id: 1});
sinon.stub(story, "changedAttributes", function(){
return {is_read: true};
return {readed: true};
});
story.shouldSave().should.be.true;
story.changedAttributes.should.have.been.calledOnce;
Expand Down
6 changes: 3 additions & 3 deletions TODO/spec/javascript/spec/views/story_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe("Storyiew", function(){
pretty_date: "Mon July 1, 2013",
permalink: "http://example.com/krunch",
keep_unread: false,
is_read: false,
is_starred: false
readed: false,
starred: false
});

this.view = new StoryView({model: this.story})
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("Storyiew", function(){
it("should autofill star button based on item", function(){
assertTagExists(this.view.$el, ".story-starred .icon-star-empty", 2);

this.story.set("is_starred", true);
this.story.set("starred", true);
this.view.render();

assertTagExists(this.view.$el, ".story-starred .icon-star", 2);
Expand Down
26 changes: 13 additions & 13 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Story = Backbone.Model.extend({
},

open: function() {
if (!this.get("keep_unread")) this.set("is_read", true);
if (!this.get("keep_unread")) this.set("readed", true);
if (this.shouldSave()) this.save();

if(this.collection){
Expand All @@ -70,20 +70,20 @@ var Story = Backbone.Model.extend({
toggleKeepUnread: function() {
if (this.get("keep_unread")) {
this.set("keep_unread", false);
this.set("is_read", true);
this.set("readed", true);
} else {
this.set("keep_unread", true);
this.set("is_read", false);
this.set("readed", false);
}

if (this.shouldSave()) this.save();
},

toggleStarred: function() {
if (this.get("is_starred")) {
this.set("is_starred", false);
if (this.get("starred")) {
this.set("starred", false);
} else {
this.set("is_starred", true);
this.set("starred", true);
}

if (this.shouldSave()) this.save();
Expand Down Expand Up @@ -124,15 +124,15 @@ var StoryView = Backbone.View.extend({
this.listenTo(this.model, 'add', this.render);
this.listenTo(this.model, 'change:selected', this.itemSelected);
this.listenTo(this.model, 'change:open', this.itemOpened);
this.listenTo(this.model, 'change:is_read', this.itemRead);
this.listenTo(this.model, 'change:readed', this.itemRead);
this.listenTo(this.model, 'change:keep_unread', this.itemKeepUnread);
this.listenTo(this.model, 'change:is_starred', this.itemStarred);
this.listenTo(this.model, 'change:starred', this.itemStarred);
},

render: function() {
var jsonModel = this.model.toJSON();
this.$el.html(this.template(jsonModel));
if (jsonModel.is_read) {
if (jsonModel.readed) {
this.$el.addClass('read');
}
if (jsonModel.keep_unread) {
Expand All @@ -142,7 +142,7 @@ var StoryView = Backbone.View.extend({
},

itemRead: function() {
this.$el.toggleClass("read", this.model.get("is_read"));
this.$el.toggleClass("read", this.model.get("readed"));
},

itemOpened: function() {
Expand All @@ -168,7 +168,7 @@ var StoryView = Backbone.View.extend({
},

itemStarred: function() {
var icon = this.model.get("is_starred") ? "icon-star" : "icon-star-empty";
var icon = this.model.get("starred") ? "icon-star" : "icon-star-empty";
this.$(".story-starred > i").attr("class", icon);
},

Expand All @@ -177,7 +177,7 @@ var StoryView = Backbone.View.extend({
var backgroundTab = window.open(this.model.get("permalink"));
if (backgroundTab) backgroundTab.blur();
window.focus();
if (!this.model.get("keep_unread")) this.model.set("is_read", true);
if (!this.model.get("keep_unread")) this.model.set("readed", true);
if (this.model.shouldSave()) this.model.save();
} else {
this.model.toggle();
Expand Down Expand Up @@ -208,7 +208,7 @@ var StoryList = Backbone.Collection.extend({
},

unreadCount: function() {
return this.where({is_read: false}).length;
return this.where({readed: false}).length;
},

closeOthers: function(modelToSkip) {
Expand Down
2 changes: 1 addition & 1 deletion app/commands/stories/mark_all_as_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(story_ids, repository = StoryRepository)
end

def mark_as_read
@repo.fetch_by_ids(@story_ids).update_all(is_read: true)
@repo.fetch_by_ids(@story_ids).update_all(readed: true)
end
end
2 changes: 1 addition & 1 deletion app/commands/stories/mark_as_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(story_id, repository = StoryRepository)
end

def mark_as_read
@repo.fetch(@story_id).update_attributes(is_read: true)
@repo.fetch(@story_id).update_attributes(readed: true)
end
end
2 changes: 1 addition & 1 deletion app/commands/stories/mark_as_starred.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(story_id, repository = StoryRepository)
end

def mark_as_starred
@repo.fetch(@story_id).update_attributes(is_starred: true)
@repo.fetch(@story_id).update_attributes(starred: true)
end
end
2 changes: 1 addition & 1 deletion app/commands/stories/mark_as_unread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(story_id, repository = StoryRepository)
end

def mark_as_unread
@repo.fetch(@story_id).update_attributes(is_read: false)
@repo.fetch(@story_id).update_attributes(readed: false)
end
end
2 changes: 1 addition & 1 deletion app/commands/stories/mark_as_unstarred.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(story_id, repository = StoryRepository)
end

def mark_as_unstarred
@repo.fetch(@story_id).update_attributes(is_starred: false)
@repo.fetch(@story_id).update_attributes(starred: false)
end
end
2 changes: 1 addition & 1 deletion app/commands/stories/mark_feed_as_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def initialize(feed_id, timestamp, repository = StoryRepository)
end

def mark_feed_as_read
@repo.fetch_unread_for_feed_by_timestamp(@feed_id, @timestamp).update_all(is_read: true)
@repo.fetch_unread_for_feed_by_timestamp(@feed_id, @timestamp).update_all(readed: true)
end
end
4 changes: 2 additions & 2 deletions app/commands/stories/mark_group_as_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def mark_group_as_read
return unless @group_id

if [KINDLING_GROUP_ID, SPARKS_GROUP_ID].include?(@group_id.to_i)
@repo.fetch_unread_by_timestamp(@timestamp).update_all(is_read: true)
@repo.fetch_unread_by_timestamp(@timestamp).update_all(readed: true)
elsif @group_id.to_i > 0
@repo.fetch_unread_by_timestamp_and_group(@timestamp, @group_id).update_all(is_read: true)
@repo.fetch_unread_by_timestamp_and_group(@timestamp, @group_id).update_all(readed: true)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/archived_news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ArchivedNewsController < ApplicationController
def index
@read_stories = current_user.stories
.where(is_read: true)
.where(readed: true)
.includes(:feed)
.order(published: :desc)
.page(params[:page]).per(20)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/feed_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def show

@stories = @feed.stories.order(published: :desc).includes(:feed)

@unread_stories = @stories.where(is_read: false)
@unread_stories = @stories.where(readed: false)
end
end
3 changes: 1 addition & 2 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class NewsController < ApplicationController
def index
@unread_stories = current_user.stories
.where(is_read: false)
@unread_stories = current_user.unreaded_stories
.order(published: :desc)
.includes(:feed)
.page(params[:page]).per(20)
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/starred_news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class StarredNewsController < ApplicationController
def index
@starred_stories = current_user.stories
.where(is_starred: true)
@starred_stories = current_user.starred_stories
.includes(:feed)
.order(published: :desc)
.page(params[:page]).per(20)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def update
json_params = JSON.parse(request.body.read, symbolize_names: true)

story = StoryRepository.fetch(params[:id])
story.is_read = !!json_params[:is_read]
story.readed = !!json_params[:readed]
story.keep_unread = !!json_params[:keep_unread]
story.is_starred = !!json_params[:is_starred]
story.starred = !!json_params[:starred]

StoryRepository.save(story)

Expand Down
2 changes: 1 addition & 1 deletion app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Feed < ApplicationRecord

has_many :stories, -> { order('published desc') }, dependent: :destroy

has_many :unread_stories, -> { where(is_read: false).order('published desc') }, class_name: 'Story'
has_many :unread_stories, -> { where(readed: false).order('published desc') }, class_name: 'Story'

validates :url, presence: true

Expand Down
4 changes: 2 additions & 2 deletions app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def as_fever_json
author: source,
html: body,
url: permalink,
is_saved: is_starred ? 1 : 0,
is_read: is_read ? 1 : 0,
is_saved: starred ? 1 : 0,
is_read: readed ? 1 : 0,
created_on_time: published.to_i
}
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ class User < ApplicationRecord

has_many :stories, through: :feeds

has_many :unreaded_stories, -> { where(readed: false) },
through: :feeds,
source: :stories

has_many :starred_stories, -> { where(starred: true) },
through: :feeds,
source: :stories

has_secure_password
end
Loading