segments 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'jeweler'
5
+ gem 'git'
6
+ end
@@ -0,0 +1,16 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.2)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.1)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ git
16
+ jeweler
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Josh McArthur
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ Segments
2
+ ========
3
+
4
+ A Rails 3 Engine for managing and rendering text segments into your Rails web application. Segments are short-to-medium blocks of text or HTML that you wish to use throughout your application. It includes a web interface for managing segments, and is automatically compatible with Internationalization (I18n).
5
+
6
+ Features
7
+ --------
8
+
9
+ * Rails 3 Engine
10
+ * Internationalized web interface
11
+ * Customizable
12
+ * Supports segments in multiple languages
13
+ * ActiveRecord backend
14
+
15
+ Installation
16
+ ------------
17
+
18
+ 1. Add Segments to your Gemfile
19
+ `gem 'segments'` (Stable)
20
+ `gem 'segments', :git => 'https://github.com/joshmcarthur/segments.git`
21
+ 2. Install the engine
22
+ `bundle install`
23
+ `rails generate segments`
24
+ 3. Migrate database
25
+ `rake db:migrate`
26
+
27
+ Usage
28
+ -----
29
+
30
+ * Managing segments:
31
+ * Start your application, and visit /segments to manage segments
32
+ * A segment has a key (Like an Internationalization key, except a string), content and a locale for which it should be rendered (I18n's default locale is the default). Keys can be the same, as long as the locale is different. This effectively means that you can have the same segment, for different languages.
33
+ * If you require this segment page to be secured, you can override the authenticate_segments method of the SegmentsController to authenticate users.
34
+ * Displaying segments:
35
+ * A helper method called render_segment is available to all views. This block accpets a single argument, which is the key of the segment to render. The code that finds the segment uses I18n's current locale, so as long as this is set correctly, the correct segment will be rendered.
36
+
37
+ Contributing
38
+ ------------
39
+
40
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
41
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
42
+ * Fork the project
43
+ * Start a feature/bugfix branch
44
+ * Commit and push until you are happy with your contribution
45
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
46
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
47
+
48
+ Copyright
49
+ ---------
50
+
51
+ Copyright (c) 2011 Josh McArthur. See LICENSE.txt for
52
+ further details.
53
+
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "segments"
18
+ gem.homepage = "http://github.com/joshmcarthur/segments"
19
+ gem.version = File.exists?('VERSION') ? File.read('VERSION') : ''
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{A Rails 3 Engine for managing text segments}
22
+ gem.description = %Q{A Rails 3 Engine for managing and rendering text segments into your Rails web application. Segments are short-to-medium blocks of text or HTML that you wish to use throughout your application. It includes a web interface for managing segments, and is automatically compatible with Internationalization (I18n)}
23
+ gem.email = "joshua.mcarthur@gmail.com"
24
+ gem.authors = ["Josh McArthur"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,40 @@
1
+ class SegmentsController < ApplicationController
2
+ respond_to(:html, :json, :xml)
3
+
4
+ layout 'layouts/segment_layout'
5
+
6
+ #Override authenticate_for_segments to handle authentication
7
+ before_filter :authenticate_for_segments
8
+ before_filter :load_segment, :except => [:index, :new, :create]
9
+
10
+ def index
11
+ respond_with @segments = Segment.all
12
+ end
13
+
14
+ def update
15
+ respond_with @segment.update_attributes(params[:segment]), :location => segments_path
16
+ end
17
+
18
+ def create
19
+ respond_with @segment = Segment.create(params[:segment]), :location => segments_path
20
+ end
21
+
22
+ def destroy
23
+ respond_with @segment.destroy
24
+ end
25
+
26
+ def show
27
+ respond_with @segment
28
+ end
29
+
30
+
31
+ private
32
+
33
+ def authenticate_for_segments
34
+ return true
35
+ end
36
+
37
+ def load_segment
38
+ @segment = Segment.find(params[:id])
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ class Segment < ActiveRecord::Base
2
+
3
+ validates_uniqueness_of :key, :scope => :locale
4
+ validates_presence_of :content, :locale
5
+ before_validation :set_default_locale
6
+
7
+ private
8
+
9
+ def set_default_locale
10
+ return unless self.locale.nil? || self.locale.blank?
11
+ self.locale = I18n.default_locale
12
+ end
13
+
14
+ end
@@ -0,0 +1,27 @@
1
+ <html>
2
+ <head>
3
+ <title><%= t('segments.title') %></title>
4
+ <style type="text/css">
5
+ body { background: #556270; font-size: 16px; font-family: sans-serif;}
6
+ div.container { margin: 20px auto; padding: 20px; border: solid 1px #666; width: 700px; min-height: 500px; background-color: #FCFBE3; }
7
+ label { color: #333; font-size: 80%; font-style: italic; padding: 2px 0; }
8
+ fieldset { border: solid 1px #999; }
9
+ legend { font-weight: bold; }
10
+ form.button_to, form.button_to div { display: inline; }
11
+ a, form.button_to input { margin: 0; padding: 5px; color: #333; text-decoration: none; font-size: 90%; background: none; border: 0; }
12
+ table { width: 100%; border-collapse: collapse; }
13
+ th { text-align: left; }
14
+ td.locale { width: 20%; }
15
+ td.key { width: 50%; }
16
+ td { width: 5%; }
17
+ tr.segment:hover { background-color: #CCC;}
18
+ a:hover, form.button_to input:hover { background-color: #556270; color: #FFF; }
19
+ </style>
20
+ </head>
21
+
22
+ <body>
23
+ <div class="container">
24
+ <%= yield %>
25
+ </div>
26
+ </body>
27
+ </html>
@@ -0,0 +1,19 @@
1
+
2
+ <p>
3
+ <%= form.label :locale -%><br />
4
+ <%= form.select :locale, I18n.available_locales -%>
5
+ </p>
6
+
7
+ <p>
8
+ <%= form.label :key -%><br />
9
+ <%= form.text_field :key -%>
10
+ </p>
11
+
12
+ <p>
13
+ <%= form.label :content -%><br />
14
+ <%= form.text_area :content, :rows => 5 -%>
15
+ </p>
16
+
17
+ <p>
18
+ <%= form.submit %>
19
+ </p>
@@ -0,0 +1,7 @@
1
+ <tr class="segment">
2
+ <td class="locale"><%= segment.locale -%></td>
3
+ <td class="key"><%= segment.key -%></td>
4
+ <td><%= link_to t('segments.show_link'), segment_url(segment) -%></td>
5
+ <td><%= link_to t('segments.edit_link'), edit_segment_url(segment) -%></td>
6
+ <td><%= button_to t('segments.destroy_link'), segment_url(segment), :method => :delete -%></td>
7
+ </tr>
@@ -0,0 +1,4 @@
1
+ <%= content_tag :h1, t('segments.edit.heading', :key => @segment.key, :language => @segment.locale) -%>
2
+ <%= form_for @segment do |form| %>
3
+ <%= render :partial => 'form', :locals => {:form => form} -%>
4
+ <% end -%>
@@ -0,0 +1,22 @@
1
+ <%= content_tag :h1, t('segments.index.heading') -%>
2
+ <%= form_for Segment.new do |form| %>
3
+ <fieldset>
4
+ <legend><%= t('segments.new.form.legend') -%></legend>
5
+ <%= render :partial => 'form', :locals => {:form => form} -%>
6
+ </fieldset>
7
+ <% end -%>
8
+
9
+ <table>
10
+ <thead>
11
+ <tr>
12
+ <th>Locale</th>
13
+ <th>Key</th>
14
+ <th></th>
15
+ <th></th>
16
+ </tr>
17
+ </thead>
18
+
19
+ <tbody>
20
+ <%= render :partial => 'segment_row', :collection => @segments, :as => :segment -%>
21
+ </tbody
22
+ </table>
@@ -0,0 +1,6 @@
1
+ <%= content_tag :h1, t('segments.show.heading', {:language => @segment.locale , :key => @segment.key}) -%>
2
+
3
+ <%= content_tag :div, @segment.content %>
4
+
5
+ <%= link_to t('segments.back_link'), segments_url -%>
6
+ <%= link_to t('segments.edit_link'), edit_segment_url(@segment) -%>
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
5
+ begin
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Try running `bundle install`."
12
+ exit!
13
+ end if File.exist?(gemfile)
@@ -0,0 +1,17 @@
1
+ en:
2
+ segments:
3
+ title: Manage Segments
4
+ index:
5
+ heading: Segments
6
+ show_link: View
7
+ edit_link: Edit
8
+ destroy_link: Delete
9
+ back_link: Back
10
+ edit:
11
+ heading: Editing segment %{key} (%{language})
12
+ show:
13
+ heading: %{key} (%{language})
14
+ new:
15
+ form:
16
+ legend: New Segment
17
+
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ # Add your engine routes here
3
+ resources :segments
4
+ end
@@ -0,0 +1,24 @@
1
+ module Segments
2
+ class Engine < Rails::Engine
3
+ config.autoload_paths += %W(#{config.root}/lib)
4
+
5
+ def self.activate
6
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
7
+ Rails.env.production? ? require(c) : load(c)
8
+ end
9
+
10
+ ActionView::Rendering.module_eval do
11
+ def render_segment(segment_name = "")
12
+ segment = Segment.find_by_key_and_locale(
13
+ segment_name,
14
+ I18n.locale
15
+ )
16
+ render :text => segment.content.html_safe if segment
17
+ end
18
+ end
19
+ end
20
+
21
+ config.to_prepare &method(:activate).to_proc
22
+ end
23
+ end
24
+
@@ -0,0 +1,26 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class SegmentsGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
15
+ def self.next_migration_number(dirname)
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
18
+ else
19
+ "%.3d" % (current_migration_number(dirname) + 1)
20
+ end
21
+ end
22
+
23
+ def create_migration_file
24
+ migration_template 'segments_migration.rb', 'db/migrate/create_segments.rb'
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSegments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :segments do |t|
4
+ t.string :key, :null => false
5
+ t.string :locale, :null => false
6
+ t.text :content
7
+ end
8
+
9
+ add_index :segments, :key, :unique => true
10
+ end
11
+
12
+ def self.down
13
+ drop_index :segments, :key
14
+ drop_table :segments
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ module Segments
2
+ require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
3
+ end
@@ -0,0 +1 @@
1
+ # add custom rake tasks here
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{segments}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Josh McArthur"]
12
+ s.date = %q{2011-06-04}
13
+ s.description = %q{A Rails 3 Engine for managing and rendering text segments into your Rails web application. Segments are short-to-medium blocks of text or HTML that you wish to use throughout your application. It includes a web interface for managing segments, and is automatically compatible with Internationalization (I18n)}
14
+ s.email = %q{joshua.mcarthur@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "app/controllers/segments_controller.rb",
28
+ "app/models/segment.rb",
29
+ "app/views/layouts/segment_layout.html.erb",
30
+ "app/views/segments/_form.html.erb",
31
+ "app/views/segments/_segment_row.html.erb",
32
+ "app/views/segments/edit.html.erb",
33
+ "app/views/segments/index.html.erb",
34
+ "app/views/segments/show.html.erb",
35
+ "config/boot.rb",
36
+ "config/locales/en.yml",
37
+ "config/routes.rb",
38
+ "lib/engine.rb",
39
+ "lib/generators/segments/segments_generator.rb",
40
+ "lib/generators/segments/templates/segments_migration.rb",
41
+ "lib/segments.rb",
42
+ "lib/tasks/segments.rake",
43
+ "segments.gemspec"
44
+ ]
45
+ s.homepage = %q{http://github.com/joshmcarthur/segments}
46
+ s.licenses = ["MIT"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.7.2}
49
+ s.summary = %q{A Rails 3 Engine for managing text segments}
50
+
51
+ if s.respond_to? :specification_version then
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
56
+ s.add_development_dependency(%q<git>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<jeweler>, [">= 0"])
59
+ s.add_dependency(%q<git>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<jeweler>, [">= 0"])
63
+ s.add_dependency(%q<git>, [">= 0"])
64
+ end
65
+ end
66
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: segments
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Josh McArthur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-04 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeweler
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: git
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ description: A Rails 3 Engine for managing and rendering text segments into your Rails web application. Segments are short-to-medium blocks of text or HTML that you wish to use throughout your application. It includes a web interface for managing segments, and is automatically compatible with Internationalization (I18n)
38
+ email: joshua.mcarthur@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE.txt
45
+ - README.md
46
+ files:
47
+ - .document
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - VERSION
54
+ - app/controllers/segments_controller.rb
55
+ - app/models/segment.rb
56
+ - app/views/layouts/segment_layout.html.erb
57
+ - app/views/segments/_form.html.erb
58
+ - app/views/segments/_segment_row.html.erb
59
+ - app/views/segments/edit.html.erb
60
+ - app/views/segments/index.html.erb
61
+ - app/views/segments/show.html.erb
62
+ - config/boot.rb
63
+ - config/locales/en.yml
64
+ - config/routes.rb
65
+ - lib/engine.rb
66
+ - lib/generators/segments/segments_generator.rb
67
+ - lib/generators/segments/templates/segments_migration.rb
68
+ - lib/segments.rb
69
+ - lib/tasks/segments.rake
70
+ - segments.gemspec
71
+ homepage: http://github.com/joshmcarthur/segments
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 285984097
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.7.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: A Rails 3 Engine for managing text segments
101
+ test_files: []
102
+