Skip to content

Commit

Permalink
Move schemas to lib/tilex
Browse files Browse the repository at this point in the history
  • Loading branch information
vnegrisolo committed Oct 24, 2022
1 parent 4212768 commit 54bae62
Show file tree
Hide file tree
Showing 42 changed files with 104 additions and 86 deletions.
6 changes: 3 additions & 3 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import_file_if_available("~/.iex.exs")

alias Tilex.Channel
alias Tilex.Developer
alias Tilex.Blog.Channel
alias Tilex.Blog.Developer
alias Tilex.Liking
alias Tilex.Post
alias Tilex.Blog.Post
alias Tilex.Rep

# Allow developer to reload IEx session with `R.reload!`.
Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks/ecto.twiki.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule Mix.Tasks.Ecto.Twiki do
use Mix.Task

alias Ecto.{Migrator}
alias Mix.{Ecto, Project, EctoSQL}
alias Ecto.Migrator
alias Mix.Ecto
alias Mix.Project
alias Mix.EctoSQL

@shortdoc "Ecto Migration: Up, Down, Up"

Expand Down
3 changes: 2 additions & 1 deletion lib/tilex/auth/guardian.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Tilex.Auth.Guardian do
use Guardian, otp_app: :tilex

alias Tilex.{Developer, Repo}
alias Tilex.Blog.Developer
alias Tilex.Repo

def subject_for_token(%Developer{} = developer, _claims), do: {:ok, "Developer:#{developer.id}"}
def subject_for_token(_resource, _claims), do: {:error, "Unknown resource type"}
Expand Down
8 changes: 5 additions & 3 deletions lib/tilex_web/models/channel.ex → lib/tilex/blog/channel.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Tilex.Channel do
use TilexWeb, :schema
defmodule Tilex.Blog.Channel do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query

@type t :: module

alias Tilex.Post
alias Tilex.Blog.Post

schema "channels" do
field(:name, :string)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Tilex.Developer do
use TilexWeb, :schema
defmodule Tilex.Blog.Developer do
use Ecto.Schema
import Ecto.Changeset

@type t :: module

alias Tilex.{Developer, Post}
alias Tilex.Blog.Developer
alias Tilex.Blog.Post

schema "developers" do
field(:email, :string)
Expand Down
9 changes: 6 additions & 3 deletions lib/tilex_web/models/post.ex → lib/tilex/blog/post.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
defmodule Tilex.Post do
use TilexWeb, :schema
defmodule Tilex.Blog.Post do
use Ecto.Schema
import Ecto.Changeset

@type t :: module

alias Tilex.{Developer, Channel, Post}
alias Tilex.Blog.Developer
alias Tilex.Blog.Channel
alias Tilex.Blog.Post

@body_max_words 200
def body_max_words, do: @body_max_words
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex_web/models/request.ex → lib/tilex/blog/request.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Tilex.Request do
use TilexWeb, :schema
defmodule Tilex.Blog.Request do
use Ecto.Schema

@type t :: module

Expand Down
4 changes: 3 additions & 1 deletion lib/tilex/liking.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Tilex.Liking do
alias Tilex.{Notifications, Post, Repo}
alias Tilex.Notifications
alias Tilex.Blog.Post
alias Tilex.Repo

def like(slug) do
post = Repo.get_by!(Post, slug: slug)
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex/notifications/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Tilex.Notifications do
use GenServer

alias Ecto.Changeset
alias Tilex.Post
alias Tilex.Blog.Post
alias Tilex.Repo
alias TilexWeb.Endpoint
alias TilexWeb.Router.Helpers, as: Routes
Expand Down
4 changes: 3 additions & 1 deletion lib/tilex/notifications/notifiers/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ defmodule Tilex.Notifications.Notifier do
Tilex.Notifications.NotifiersSupervisor.children/1
"""

alias Tilex.{Post, Developer, Channel}
alias Tilex.Blog.Post
alias Tilex.Blog.Developer
alias Tilex.Blog.Channel

@callback handle_post_created(Post.t(), Developer.t(), Channel.t(), url :: String.t()) :: any
@callback handle_post_liked(Post.t(), Developer.t(), url :: String.t()) :: any
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex/notifications/notifiers/slack.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Tilex.Notifications.Notifiers.Slack do
use Tilex.Notifications.Notifier

alias Tilex.Post
alias Tilex.Blog.Post

@emoji ~w(
:tada:
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex/notifications/notifiers/twitter.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Tilex.Notifications.Notifiers.Twitter do
alias Tilex.Developer
alias Tilex.Blog.Developer

use Tilex.Notifications.Notifier

Expand Down
5 changes: 4 additions & 1 deletion lib/tilex/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ defmodule Tilex.Posts do
import Ecto.Query

alias Ecto.Adapters.SQL
alias Tilex.{Channel, Developer, Post, Repo}
alias Tilex.Blog.Channel
alias Tilex.Blog.Developer
alias Tilex.Blog.Post
alias Tilex.Repo

def all(page) do
page
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Tilex.Stats do

alias Ecto.Adapters.SQL
alias Tilex.Repo
alias Tilex.Channel
alias Tilex.Blog.Channel

def developer(%{start_date: start_date, end_date: end_date}) do
start_time = Timex.to_datetime(start_date)
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex/tracking.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Tilex.Tracking do
alias Tilex.Repo
alias Tilex.Request
alias Tilex.Blog.Request
alias TilexWeb.Endpoint

import Ecto.Query, only: [from: 2, subquery: 1]
Expand Down Expand Up @@ -36,7 +36,7 @@ defmodule Tilex.Tracking do
query =
from(
req in subquery(requests),
join: post in Tilex.Post,
join: post in Tilex.Blog.Post,
on: [slug: req.url_slug],
order_by: [desc: req.view_count],
limit: 10,
Expand Down
8 changes: 0 additions & 8 deletions lib/tilex_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ defmodule TilexWeb do
end
end

def schema do
quote do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
end
end

defp view_helpers do
quote do
# Use all HTML functionality (forms, tags, etc)
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule TilexWeb.UserSocket do
use Phoenix.Socket

## Channels
channel("post:search", Tilex.PostChannel)
channel("post:search", Tilex.Blog.PostChannel)
channel("text_converter", Tilex.TextConverterChannel)

# Socket params are passed from the client and can
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/api/developer_post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule TilexWeb.Api.DeveloperPostController do
"""

use TilexWeb, :controller
alias Tilex.{Posts}
alias Tilex.Posts

@doc """
This function allows external requesters to retrieve a developer's three most
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/api/post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TilexWeb.Api.PostController do

use TilexWeb, :controller
use Tilex.Pageable
alias Tilex.{Posts}
alias Tilex.Posts

@doc """
This functions allows external requesters to retrieve the feed of til in json format
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule TilexWeb.AuthController do
use TilexWeb, :controller
plug(Ueberauth)

alias Tilex.Developer
alias Tilex.Blog.Developer
alias Tilex.Repo
alias Tilex.Auth

Expand Down
5 changes: 4 additions & 1 deletion lib/tilex_web/controllers/developer_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
defmodule TilexWeb.DeveloperController do
use TilexWeb, :controller

alias Tilex.{Developer, Posts, Repo, Auth}
alias Tilex.Blog.Developer
alias Tilex.Posts
alias Tilex.Repo
alias Tilex.Auth

def show(conn, %{"name" => username} = params) do
page =
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/feed_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TilexWeb.FeedController do
posts =
Repo.all(
from(
p in Tilex.Post,
p in Tilex.Blog.Post,
order_by: [desc: p.inserted_at],
preload: [:channel, :developer],
limit: 25
Expand Down
6 changes: 5 additions & 1 deletion lib/tilex_web/controllers/post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ defmodule TilexWeb.PostController do
import Ecto.Query

alias Guardian.Plug
alias Tilex.{Channel, Notifications, Liking, Post, Posts}
alias Tilex.Blog.Channel
alias Tilex.Notifications
alias Tilex.Liking
alias Tilex.Blog.Post
alias Tilex.Posts

plug(:load_channels when action in [:new, :create, :edit, :update])
plug(:extract_slug when action in [:show, :edit, :update])
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/sitemap_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TilexWeb.SitemapController do

def index(conn, _) do
conn
|> assign(:posts, Repo.all(Tilex.Post))
|> assign(:posts, Repo.all(Tilex.Blog.Post))
|> put_layout(false)
|> render("sitemap.xml")
end
Expand Down
4 changes: 3 additions & 1 deletion lib/tilex_web/controllers/test/auth_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule TilexWeb.Test.AuthController do
use TilexWeb, :controller

alias Tilex.{Developer, Repo, Auth}
alias Tilex.Blog.Developer
alias Tilex.Repo
alias Tilex.Auth

def index(conn, params) do
developer = Repo.get_by!(Developer, id: params["id"])
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex_web/templates/post/form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<dl>
<dt>
<%= label f, :title %> <%= error_tag f, :title %>
<small class="character_limit" data-limit="<%= Tilex.Post.title_max_chars %>"></small>
<small class="character_limit" data-limit="<%= Tilex.Blog.Post.title_max_chars %>"></small>
</dt>
<dd>
<%= text_input f, :title, placeholder: "Enter title..." %>
Expand All @@ -11,7 +11,7 @@
<dl>
<dt>
<%= label f, :body %> <%= error_tag f, :body %>
<small class="word_limit" data-limit="<%= Tilex.Post.body_max_words %>"></small>
<small class="word_limit" data-limit="<%= Tilex.Blog.Post.body_max_words %>"></small>
</dt>
<dd>
<%= textarea f, :body,
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/templates/shared/post.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<a href='http://twitter.com/share'
class='twitter-share-button'
data-text= "Today I learned: <%= @post.title %>"
data-via= "<%= Tilex.Developer.twitter_handle(@post.developer) %>"
data-via= "<%= Tilex.Blog.Developer.twitter_handle(@post.developer) %>"
data-hashtags= "<%= @post.channel.twitter_hashtag %>"
data-url= "<%= Routes.post_url(@conn, :show, @post) %>">
Tweet
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex_web/templates/stats/developer.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<ul class="post_list">
<%= for {post_title, post_likes, post_slug, channel_name, _score} <- @hottest_posts do %>
<li>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Post{slug: post_slug, title: post_title})) do %>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Blog.Post{slug: post_slug, title: post_title})) do %>
<b>
<%= post_title %>
</b>
Expand All @@ -66,7 +66,7 @@
<ul class="post_list">
<%= for {post_title, post_likes, post_slug, channel_name} <- @most_liked_posts do %>
<li>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Post{slug: post_slug, title: post_title})) do %>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Blog.Post{slug: post_slug, title: post_title})) do %>
<b>
<%= post_title %>
</b>
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex_web/templates/stats/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ul class="post_list">
<%= for {post_title, post_likes, post_slug, channel_name, _score} <- @hottest_posts do %>
<li>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Post{slug: post_slug, title: post_title})) do %>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Blog.Post{slug: post_slug, title: post_title})) do %>
<b>
<%= post_title %>
</b>
Expand All @@ -43,7 +43,7 @@
<ul class="post_list">
<%= for {post_title, post_likes, post_slug, channel_name} <- @most_liked_posts do %>
<li>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Post{slug: post_slug, title: post_title})) do %>
<%= link(to: Routes.post_path(@conn, :show, %Tilex.Blog.Post{slug: post_slug, title: post_title})) do %>
<b>
<%= post_title %>
</b>
Expand Down
10 changes: 5 additions & 5 deletions lib/tilex_web/views/layout_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule TilexWeb.LayoutView do
def page_title(%{page_title: page_title}), do: page_title
def page_title(_), do: Application.get_env(:tilex, :organization_name)

def twitter_image_url(%Tilex.Post{} = post) do
def twitter_image_url(%Tilex.Blog.Post{} = post) do
channel_name = channel_name(post)

case File.exists?("assets/static/assets/#{channel_name}_twitter_card.png") do
Expand All @@ -48,16 +48,16 @@ defmodule TilexWeb.LayoutView do
end
end

def twitter_title(%Tilex.Post{} = post) do
Tilex.Post.twitter_title(post)
def twitter_title(%Tilex.Blog.Post{} = post) do
Tilex.Blog.Post.twitter_title(post)
end

def twitter_title(_post) do
"Today I Learned: a Hashrocket Project"
end

def twitter_description(%Tilex.Post{} = post) do
markdown = Tilex.Post.twitter_description(post)
def twitter_description(%Tilex.Blog.Post{} = post) do
markdown = Tilex.Blog.Post.twitter_description(post)

earmark_options = %Earmark.Options{pure_links: false}

Expand Down
8 changes: 4 additions & 4 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Tilex.Repo.insert!(%Tilex.SomeModel{})
# Tilex.Repo.insert!(%Tilex.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.

alias Tilex.Channel
alias Tilex.Developer
alias Tilex.Post
alias Tilex.Blog.Channel
alias Tilex.Blog.Developer
alias Tilex.Blog.Post
alias Tilex.Repo

Repo.delete_all(Post)
Expand Down
Loading

0 comments on commit 54bae62

Please sign in to comment.