tags snippets

"updating paths is incompatible with switching branches/forcing" error when using git branches with Vlad

Tagged vlad, git, branch, deploy, revisions, tags  Languages 

I ended up writing this snippet after I found out that deploying a git branch with vlad wasn't as easy as it should've been...

I assumed the Vlad documentation was right, so I put the branch name in the revision variable (located in config/deploy.rb):

set :revision,              'branch_name'

But then I got this error when executing rake vlad:deploy:

git checkout: updating paths is incompatible with switching branches/forcing
Did you intend to checkout 'branch_name' which can not be resolved as commit?

I managed to solve the problem by using the SHA1 hash of the branch instead of the branch name, but that only worked for the current revision:

set :revision,              'd34360870be6536992e6d45bb0aa72eca31e14443'

So after some googling I found this post at scie.nti.st, which made my day because it has the following examples of how to deploy tags and branches with git and Vlad:

# Deploy the latest code, this the default
set :revision, "HEAD" 

# Deploy branch "origin/branch_name"
set :revision, "origin/branch_name"
 
# Deploy tag "1.0"
set :revision, "1.0"

Note that you have to push the branch to the remote server before running rake vlad:deploy:

git push origin branch_name

Atom template Rails builder template

Tagged atom, builder, category, tags  Languages ruby
atom_feed(:url => formatted_posts_url(:atom)) do |feed|
  feed.title(@category.name)
  feed.updated(@posts.first ? @posts.first.created_at : Time.now.utc)

  for post in @posts
    feed.entry(post) do |entry|
      entry.title(post.title)
      entry.content(post.body_html, :type => 'html')
      entry.updated post.updated_at

      for tag in post.tags
        entry.category :term => url_for(tag), :label => tag.name
      end
    end
  end
end

How to implement tagging with Ecto and Elixir

Tagged ecto, elixir, tagging, tags  Languages elixir
defmodule Snippets.Tagging do
  import Ecto.Query
  alias Ecto.Changeset
  alias Snippets.Repo
  @doc """
  Tags a model.

  ## Model

    defmodule Snippets.Tag do
      import Ecto.Query
      use Ecto.Schema

      schema "tags" do
        field :name
        many_to_many :snippets, Snippets.Snippet, join_through: "snippet_tags"
      end
    end

    defmodule Snippets.Snippet do
      import Ecto.Query
      use Ecto.Schema
      schema "snippets" do
        ...
        field :tag_list, :string, virtual: true
      end
    end

  ## Schema

    create table(:tags) do
      add :name, :text, null: false
    end
    create index(:tags, ["lower(name)"], unique: true)

    create table(:snippet_tags) do
      add :tag_id, references(:tags)
      add :snippet_id, references(:snippets)
    end
    create index(:snippet_tags, [:tag_id, :snippet_id], unique: true)

  ## Code

    snippet
    |> Changeset.cast(conn.params, @required_params, @optional_params)
    |> Tagging.changeset(Snippets.Tag, :tags, :tag_list)
    # Supports multiple types of tags
    |> Tagging.changeset(Snippets.Language, :languages, :language_list)

  """
  def changeset(changeset, model, association, tag_list_attr) do
    # Parse tags into Enum
    tag_list = changeset.changes
                |> Map.get(tag_list_attr, "")
                |> String.split(",")
                |> Enum.map(&(String.strip(&1)))
                |> Enum.reject(fn(name) -> name == "" end)
                |> Enum.map(&(String.downcase(&1)))
                |> Enum.uniq
                |> Enum.sort
    # Find existing tags
    existing_tags = from(t in model, where: t.name in ^tag_list) |> Repo.all
    # Create or find all tags
    tags = Enum.map(tag_list, fn(name) ->
      # Initialize new tag. Equivalent to:
      # new_tag = %Snippets.Tag{name: name}
      new_tag = struct(model, name: name)
      tag = Enum.find(existing_tags, new_tag, fn(existing_tag) ->
        existing_tag.name == name
      end)
    end)
    tag_changeset = Enum.map(tags, &Ecto.Changeset.change/1)
    # Add tags to changeset
    changeset |> Changeset.put_assoc(association, tag_changeset)
  end
end