Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/abeidahmed/micro-reddit

A simple rails console app based on the popular reddit platform.
https://github.com/abeidahmed/micro-reddit

rails reddit

Last synced: 9 days ago
JSON representation

A simple rails console app based on the popular reddit platform.

Awesome Lists containing this project

README

        

# Micro Reddit

A simple rails console app based on the [reddit](https://reddit.com) platform.

## Getting started

- Clone the repo `https://github.com/abeidahmed/micro-reddit.git`
- `cd` into the directory
- Run `git checkout -b reddit-model-feature`
- Run `git pull origin reddit-model-feature`
- Run `bundle install`
- Setup your db, and run `rails db:create db:migrate`

## Features

- Users can register themselves
- Create a post
- Comment on a post
- Comment on a comment
- Reddit style upvotes and downvotes on both posts and comments
- Bookmark posts and comments

## Usage

The first thing that you need to do is to fire up a `rails console`.

Let's get started by creating a `user`.

```ruby
user = User.create!(full_name: 'John Doe', email_address: '[email protected]', password: 'secretpassword')
=> #

# Try quering `user.password`, you'll probably get `nil`. This is because the password's are hashed before storing.
```

> Before storing the `email_address`, they are normalized and checked for a valid `email_address`.

Now, let's create a post. We will reference the above created `user` when creating a `post`.

```ruby
post = user.posts.create!(title: 'My first blog post', content: 'Hello world, an awesome day indeed')
=> #
```

> This creates a `post` and the `user_id` column is populated with the `user`'s id.

Now, let's comment on the post that we created above. Because that's what reddit does right?

```ruby
comment = post.comments.create!(content: 'Wow, an awesome post', user: user)
=> #
```

> Comment is created and the `post_id` and the `user_id` is populated automatically.

You want to comment on a comment? No problem

```ruby
nested_comment = comment.comments.create!(content: 'I am a nested comment', user: user)
=> #
```

Phew, we did a lot. But we still have more to go. Let's try the upvote and the downvote feature.

```ruby
post.upvote
=> #

# This creates a `vote` instance on the `post`.

post.votes_count
# 1

post.downvote(1) # Here 1 is the `id` of the vote that we created earlier. Normally, we could have used the controller to fetch the params and pass in the argument.
# DELETE FROM "votes" WHERE "votes"."id" = $1 [["id", 1]]

post.votes_count
# 0
```

Similarly, lets vote on the comment

```ruby
comment.upvote
=> #

comment.votes_count
# 1

# and

comment.downvote(2) # The `id` may change here. Please be cautious.
=> #

comment.votes_count
# 0
```

> The count feature is handled by the [`counter_cache`](https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html) column.

Similarly, lets vote on a nested comment

```ruby
nested_comment.upvote
=> #

# and

nested_comment.downvote(3) # The `id` may change here. Please be cautious.
# DELETE FROM "votes" WHERE "votes"."id" = $1 [["id", 3]]
```

Last but not the least, the bookmark or the save feature as reddit likes to call it.

```ruby
post.bookmarks.create! user: user
=> #

comment.bookmarks.create! user: user
=> #

nested_comment.bookmarks.create! user: user
=> #
```

And similarly you can `update` and `destroy` the records accordingly.

## Authors

### Abeid Ahmed

- GitHub: [@abeidahmed](https://github.com/abeidahmed)
- Twitter: [@iamhawaabi](https://twitter.com/iamhawaabi)
- LinkedIn: [Abeid Ahmed](https://www.linkedin.com/in/abeidahmed/)

### Martin Nyagah

- GitHub: [@menyagah](https://github.com/menyagah)
- Twitter: [@Martinnyaga20](https://twitter.com/Martinnyaga20)
- LinkedIn: [Martin Nyagah](https://www.linkedin.com/in/martin-nyagah-a29b8610b/)

## Contributing

- Fork the project
- Create your feature branch `git checkout -b awesome-feature`
- Commit your changes `git commit -m 'Awesome feature'`
- Push it `git push -u origin awesome-feature`
- Open a pull request using this branch

## License

[MIT](https://github.com/abeidahmed/tic-tac-toe/blob/development/LICENSE) licensed software.