
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>sinatra | monolog</title>
	<atom:link href="https://blog.monora.me/tag/sinatra/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.monora.me</link>
	<description>monora log #=&#62; ものろーぐ</description>
	<lastBuildDate>Sun, 24 May 2020 19:05:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.1</generator>
<site xmlns="com-wordpress:feed-additions:1">21279848</site>	<item>
		<title>Sinatra Modular-Application で configure が上書きされる問題の対処</title>
		<link>https://blog.monora.me/2016/03/problem-about-overwritten-configure-in-sinatra-using-modular-application/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=problem-about-overwritten-configure-in-sinatra-using-modular-application</link>
		
		<dc:creator><![CDATA[きょんたん]]></dc:creator>
		<pubDate>Tue, 15 Mar 2016 20:35:15 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sinatra]]></category>
		<guid isPermaLink="false">http://blog.monora.me/?p=1322</guid>

					<description><![CDATA[<p>Ruby の Siantra で、Modular-Application を書いていたら良く分からない挙動にぶち当たって気が付いたら朝になっていたのでメモ。 app.rb require_relative "hoge"  [&#8230;]</p>
The post <a href="https://blog.monora.me/2016/03/problem-about-overwritten-configure-in-sinatra-using-modular-application/">Sinatra Modular-Application で configure が上書きされる問題の対処</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></description>
										<content:encoded><![CDATA[<p>Ruby の <a href="http://www.sinatrarb.com/" target="_blank" rel="noopener noreferrer">Siantra</a> で、Modular-Application を書いていたら良く分からない挙動にぶち当たって気が付いたら朝になっていたのでメモ。</p>
<p>app.rb</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby" data-enlighter-linenumbers="true">require_relative "hoge"

class App &lt; Sinatra::Base
  configure do
    disable :protection
  end

  use HogeRoutes
end
</pre>
<p>hoge.rb</p>
<pre class="lang:ruby" title="hoge.rb">class HogeRoutes &lt; Sinatra::Base
  get "/" do
     ...
  end
end
</pre>
<p>みたいな事をしていた。<br />
<code>disable :protection</code> をしているのにどうしても <code>Rack::Protection</code> が有効になるので、どうしてこうなるんだとあちこちのコードを追いかけたりした結果、<code>HogeRoutes</code> が <code>App</code> とは別にもう一度 <code>session, protections</code> 等のセットアップが行われるので、以下のように、 <code>use HogeRoutes</code> を <code>configure</code> より上に持ってくる必要があった。</p>
<p>app.rb</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby" data-enlighter-linenumbers="true">require_relative "hoge"

class App &lt; Sinatra::Base
  use HogeRoutes

  configure do
    disable :protection
  end
end
</pre>
<p><code>HogeRoutes</code> 内の <code>configure</code> で <code>disable :protection</code> とやってもいいが、こうするとクラスが増えた際にその分だけ書かないといけなくて大変。セッションみたいなアプリケーション全体で統一的な設定を持つ部分は親の <code>App</code> でやって良いんじゃないかと思う。</p>The post <a href="https://blog.monora.me/2016/03/problem-about-overwritten-configure-in-sinatra-using-modular-application/">Sinatra Modular-Application で configure が上書きされる問題の対処</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1322</post-id>	</item>
		<item>
		<title>sinatra-activerecord で seed_fu を使ってみた</title>
		<link>https://blog.monora.me/2015/10/use-seed_fu-in-sinatra-with-sinatra-activerecord/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=use-seed_fu-in-sinatra-with-sinatra-activerecord</link>
		
		<dc:creator><![CDATA[きょんたん]]></dc:creator>
		<pubDate>Tue, 13 Oct 2015 19:02:52 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sinatra]]></category>
		<guid isPermaLink="false">http://blog.monora.me/?p=617</guid>

					<description><![CDATA[<p>Sinatra で、sinatra-activerecord を使っているときに、$ rake db:seed すると、既に存在するレコードが重複登録される。 これ、とても一般的な問題で、 Rails だと、seed_f [&#8230;]</p>
The post <a href="https://blog.monora.me/2015/10/use-seed_fu-in-sinatra-with-sinatra-activerecord/">sinatra-activerecord で seed_fu を使ってみた</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></description>
										<content:encoded><![CDATA[<p>Sinatra で、<a href="https://github.com/janko-m/sinatra-activerecord" target="_blank" rel="noopener noreferrer">sinatra-activerecord</a> を使っているときに、<code>$ rake db:seed</code> すると、既に存在するレコードが重複登録される。</p>
<p>これ、とても一般的な問題で、 Rails だと、<a href="https://github.com/mbleigh/seed-fu" target="_blank" rel="noopener noreferrer">seed_fu</a> というのを使って解決したりするらしい。</p>
<p>Rails (ActiveRecord 4.x) で動くなら Sinatra (sinatra-activerecord) でも問題なく動くとおもいきや、<code>$ rake -T</code> しても <code>db:seed_fu</code> が表示されない……</p>
<p>ソースやらなんやら見ながら唸っていたら、どうも Rake のタスク追加を、 <a href="https://github.com/mbleigh/seed-fu/blob/master/lib/seed-fu/railtie.rb" target="_blank" rel="noopener noreferrer">Railtie を使ってやろうとしているところ</a>を発見した。<br />
どうやら Rails だと、初期化時にここのコードが走るらしいのだけれど、Sinatra にそんなものはないので当然タスクは追加されない。</p>
<p>というわけで、<code>Rakefile</code> にベタ書きしてみる。<br />
メインのアプリケーションソースが <code>./app.rb</code> にあるということになっています。(適宜書き換えてください)<br />
あと、なぜか <code>&lt;&lt;</code> が <code>&lt; &lt;</code> に置き換えられたので修正してください。</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby" data-enlighter-linenumbers="true">namespace :db do
  desc &lt; &lt;-EOS Loads seed data for the current environment. It will look for ruby seed files in (settings.root)/db/fixtures/ and (settings.root)/db/fixtures/(settings.environment)/. By default it will load any ruby files found. You can filter the files loaded by passing in the FILTER environment variable with a comma-delimited list of patterns to include. Any files not matching the pattern will not be loaded. You can also change the directory where seed files are looked for with the FIXTURE_PATH environment variable. Examples: # default, to load all seed files for the current environment rake db:seed_fu # to load seed files matching orders or customers rake db:seed_fu FILTER=orders,customers # to load files from settings.root/features/fixtures rake db:seed_fu FIXTURE_PATH=features/fixtures EOS task :seed_fu =&gt; :environment do

    if ENV["FILTER"]
      filter = /#{ENV["FILTER"].gsub(/,/, "|")}/
    end

    if ENV["FIXTURE_PATH"]
      fixture_paths = [ENV["FIXTURE_PATH"], ENV["FIXTURE_PATH"] + "/" + settings.environment.to_s]
    end

    SeedFu.seed(fixture_paths, filter)
  end

  task :load_config do
    require "./app"
  end
end

Rake::Task["db:seed_fu"].enhance(["db:load_config"])

SeedFu.fixture_paths = [
  Pathname(settings.root).join("db/fixtures").to_s,
  Pathname(settings.root).join("db/fixtures/#{settings.environment}").to_s
]
</pre>
<p>コードとしては、<a href="https://github.com/mbleigh/seed-fu/blob/master/lib/tasks/seed_fu.rake" target="_blank" rel="noopener noreferrer">seed_fu の rakeファイル</a> を元に、<a href="https://github.com/janko-m/sinatra-activerecord/blob/master/lib/sinatra/activerecord/rake/activerecord_4.rb" target="_blank" rel="noopener noreferrer">sinatra-activerecord から、データベースの接続設定を拾ってくる部分</a>を参考にしたりした。</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby" data-enlighter-linenumbers="false">Rake::Task["db:seed_fu"].enhance(["db:load_config"])
</pre>
<p>の部分がミソで、これでアプリケーションを通してデータベースに接続する必要がある。ここで2時間ぐらいハマった。</p>The post <a href="https://blog.monora.me/2015/10/use-seed_fu-in-sinatra-with-sinatra-activerecord/">sinatra-activerecord で seed_fu を使ってみた</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">617</post-id>	</item>
	</channel>
</rss>
