page_fragments 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in page_fragments.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexandr Promakh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # PageFragments
2
+
3
+ Page Fragments is gem for Ruby On Rails.
4
+ PageFragments allow you to store snippets of pages in the database and edit them in the admin application.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem "page_fragments"
11
+
12
+ First, execute
13
+
14
+ $ rails generate page_fragments:install
15
+
16
+ this create config/page_fragments.rb when you write you templates
17
+
18
+ ```ruby
19
+ PageFragment.templates do
20
+ #fragment :fragment_type, :fragment_key
21
+
22
+ fragment :string, :header
23
+ fragment :text, :about
24
+ end
25
+ ```
26
+
27
+ After edit templates config you should execute 'rake pf:init'. This create templates in your database.
28
+
29
+ And this generator create migration for PageFragment model.
30
+ Now we have to perform
31
+
32
+ $ rake db:migrate
33
+
34
+ PageFragments have standard controller and views. You can using own controller inherited from `PageFragments::PageFragmentsController`.
35
+ After this you should to edit routes.
36
+
37
+ It will copy all views to your application:
38
+
39
+ $ rails generate page_fragments:views
40
+
41
+ ## Usage
42
+
43
+ In you view:
44
+
45
+ ```erb
46
+ <%= render_page_fragment :key_of_fragment %>
47
+ ```
48
+
49
+ ## Authors
50
+
51
+ Promakh Alexandr: s-promakh@ya.ru
52
+ MyStand: http://mystand.ru/
53
+
54
+ MIT License. Copyright 2013 Promakh Alexandr.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ module PageFragments
2
+ class PageFragmentsController < ApplicationController
3
+
4
+ def index
5
+ @page_fragments = PageFragment.all
6
+ end
7
+
8
+ def edit
9
+ @page_fragment = PageFragment.find(params[:id])
10
+ end
11
+
12
+ def update
13
+ @page_fragment = PageFragment.find(params[:id])
14
+ if @page_fragment.update_attributes(params[:page_fragment])
15
+ redirect_to :action => :index
16
+ else
17
+ render :edit
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module PageFragments
2
+ module PageFragmentHelper
3
+ def render_page_fragment *keys
4
+ pf = keys.first.is_a?(PageFragments::PageFragment) ? keys.first : PageFragments::PageFragment.get(keys.flatten)
5
+ pf && pf.content.to_s.html_safe
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,123 @@
1
+ module PageFragments
2
+ class PageFragment < ActiveRecord::Base
3
+ default_scope where(:hidden => false) if self.column_names.include?('hidden')
4
+ attr_accessible :key, :content
5
+
6
+ KEY_REGEXP = /^[a-z_.]+$/
7
+ AVAILABLE_CONTENT_TYPES = [:text, :string, :integer]
8
+
9
+ before_save :validate
10
+
11
+ validates :key, :uniqueness => true, :presence => true, :format => {:with => KEY_REGEXP}
12
+
13
+ serialize :validation, Hash
14
+
15
+ def self.config &block
16
+ if PageFragment.table_exists?
17
+ if PageFragment.column_names.include?('hidden')
18
+ PageFragment.all.each { |pf| pf.hidden = true; pf.save(:validate => false) }
19
+ end
20
+ PageFragment::Configuring.instance_eval(&block)
21
+ end
22
+ end
23
+
24
+ def self.get *keys
25
+ key = PageFragment.parse_key keys.flatten
26
+ PageFragment.where(:key => key).first || raise(ActiveRecord::RecordNotFound)
27
+ end
28
+
29
+ def self.set keys, value
30
+ item = PageFragment.get(keys)
31
+ item.content = value
32
+ item.save
33
+ item
34
+ end
35
+
36
+ def to_s
37
+ I18n.t("page_fragments.#{self.key}.name")
38
+ end
39
+
40
+ def parents
41
+ names = []
42
+ self.keys.each { |k| names << [names.last, k].join(".") }
43
+ names
44
+ end
45
+
46
+ def short_name
47
+ I18n.t("page_fragments.#{self.key}.name")
48
+ end
49
+
50
+ def keys
51
+ (self.key || "").split(".")
52
+ end
53
+
54
+ def nesting_level
55
+ self.keys.size
56
+ end
57
+
58
+ def type? target
59
+ if target.is_a? Array
60
+ target.map { |t| t.to_s }.include?(self.content_type)
61
+ else
62
+ self.content_type == target.to_s
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def self.parse_key *keys
69
+ keys.flatten.map { |k| k.to_s }.join(".") #its for i18n
70
+ end
71
+
72
+ def validate
73
+ return if self.new_record?
74
+ self.validation.each do |name, options|
75
+ case options
76
+ when TrueClass
77
+ options = {}
78
+ when Hash
79
+ options
80
+ when Range, Array
81
+ options = {:in => options}
82
+ else
83
+ options = {:with => options}
84
+ end
85
+ options[:attributes] = [:content]
86
+ begin
87
+ validator_name = (name.to_s.camelize + "Validator")
88
+ validator_class = ("ActiveModel::Validations::" + validator_name).constantize
89
+ rescue
90
+ raise "can't find validator #{validator_name}"
91
+ end
92
+ validator = validator_class.new(options)
93
+ validator.validate(self)
94
+ end
95
+ self.errors.empty?
96
+ end
97
+
98
+
99
+ module Configuring
100
+ #def self.validates options, &block
101
+ # PageFragment::Configuring.instance_eval(&block)
102
+ #end
103
+
104
+ def self.fragment *args
105
+ type = args.delete(args.first)
106
+ params = args.last.is_a?(Hash) ? args.delete(args.last) : {}
107
+ keys = args.flatten
108
+ PageFragment::Configuring.init type, keys, params
109
+ end
110
+
111
+ def self.init type, keys, validation
112
+ key = PageFragment.parse_key keys.flatten
113
+ item = PageFragment.unscoped.where(:key => key).first || PageFragment.new(:key => key)
114
+ raise "unexpected type of fragment" unless AVAILABLE_CONTENT_TYPES.include? type
115
+ item.content_type = type.to_s
116
+ item.validation = validation
117
+ item.hidden = false if PageFragment.column_names.include?('hidden')
118
+ item.save
119
+ end
120
+ end
121
+ end
122
+ end
123
+
@@ -0,0 +1,11 @@
1
+ = form_for @page_fragment, :as => :page_fragment, :url => url_for(:action => :update, :id => @page_fragment.id) do |f|
2
+ %fieldset
3
+ %legend= @page_fragment.short_name
4
+
5
+ %div
6
+ = f.label :content
7
+ %br
8
+ = f.text_area :content
9
+
10
+ = f.submit "Save"
11
+ = link_to "Cancel", url_for(:action => :index)
@@ -0,0 +1,11 @@
1
+ .page-fragments
2
+ %h2="Page Fragments"
3
+ %table
4
+ %thead
5
+ %th= "Name"
6
+ %th
7
+ - @page_fragments.each do |page_fragment|
8
+ %tr
9
+ %td= page_fragment.short_name
10
+ %td
11
+ = link_to "Edit", url_for(:action => :edit, :id => page_fragment.id)
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ #Rails.application.routes.draw do
2
+ #resources :page_fragments, :only => [:index, :edit, :destroy], :controller => "page_fragments/page_fragments"
3
+ #match :hello, :to => "page_fragments/page_fragments#index"
4
+ #end
@@ -0,0 +1,43 @@
1
+ require 'rails/generators/active_record'
2
+ require 'rails/generators/migration'
3
+
4
+ module PageFragments
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ def self.source_root
10
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
11
+ end
12
+
13
+ def self.next_migration_number(dirname)
14
+ if ActiveRecord::Base.timestamped_migrations
15
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
16
+ else
17
+ "%.3d" % (current_migration_number(dirname) + 1)
18
+ end
19
+ end
20
+
21
+ def copy_templates
22
+ copy_file "templates.rb", "config/page_fragments.rb"
23
+ end
24
+
25
+ #def copy_locales
26
+ # copy_file "page_fragments.en.yml", "config/locales/page_fragments.en.yml"
27
+ #end
28
+
29
+ #def copy_initialize
30
+ # template "initialize.rb", "config/initializers/page_fragments.rb"
31
+ #end
32
+
33
+ def insert_route
34
+ route 'resources :page_fragments, :only => [:index, :edit, :update, :destroy], :controller => "page_fragments/page_fragments"'
35
+ end
36
+
37
+ def create_migration_file
38
+ migration_template 'migration.rb', 'db/migrate/create_page_fragments.rb'
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ PageFragment.setup do |config|
2
+ #config.controller = :page_fragments
3
+ #config.model = :page_fragment
4
+
5
+ end
@@ -0,0 +1,13 @@
1
+ class CreatePageFragments < ActiveRecord::Migration
2
+ def change
3
+ create_table :page_fragments do |t|
4
+ t.string :key
5
+ t.string :content_type
6
+ t.text :validation
7
+ t.text :content
8
+ t.boolean :hidden, :default => false
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ views:
3
+ page_fragments:
4
+ page_fragments:
5
+ index:
6
+ title: Page fragments
@@ -0,0 +1,3 @@
1
+ PageFragments.templates do
2
+ #fragment :fragment_type, :fragment_key
3
+ end
@@ -0,0 +1,17 @@
1
+ module PageFragments
2
+ module Generators
3
+ class ViewsGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @source_root = File.join(File.dirname(__FILE__), '../../../app/views/page_fragments/page_fragments')
8
+ end
9
+
10
+ def copy_vies
11
+ copy_file "index.html.haml", "app/views/page_fragments/page_fragments/index.html.haml"
12
+ copy_file "edit.html.haml", "app/views/page_fragments/page_fragments/edit.html.haml"
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require "rails"
2
+ require "page_fragments/version"
3
+
4
+ module PageFragments
5
+ #def self.config
6
+ # yield self
7
+ #end
8
+
9
+ def self.templates &block
10
+ self::PageFragment.config &block
11
+ end
12
+
13
+ class Railtie < Rails::Railtie
14
+ rake_tasks do
15
+ load File.join(File.dirname(__FILE__), 'tasks/pf.rake')
16
+ end
17
+ end
18
+
19
+ class Engine < Rails::Engine
20
+ end
21
+
22
+ end
@@ -0,0 +1,50 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+ def devise_for(*resources)
4
+ options = resources.extract_options!
5
+
6
+ options[:as] ||= @scope[:as] if @scope[:as].present?
7
+ options[:module] ||= @scope[:module] if @scope[:module].present?
8
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
9
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
10
+ options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
11
+ options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
12
+ options[:options] = @scope[:options] || {}
13
+ options[:options][:format] = false if options[:format] == false
14
+
15
+ resources.map!(&:to_sym)
16
+
17
+ resources.each do |resource|
18
+ mapping = Devise.add_mapping(resource, options)
19
+
20
+ begin
21
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
22
+ rescue NameError => e
23
+ raise unless mapping.class_name == resource.to_s.classify
24
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
25
+ "no model #{mapping.class_name} defined in your application"
26
+ next
27
+ rescue NoMethodError => e
28
+ raise unless e.message.include?("undefined method `devise'")
29
+ raise_no_devise_method_error!(mapping.class_name)
30
+ end
31
+
32
+ routes = mapping.used_routes
33
+
34
+ devise_scope mapping.name do
35
+ if block_given?
36
+ ActiveSupport::Deprecation.warn "Passing a block to devise_for is deprecated. " \
37
+ "Please remove the block from devise_for (only the block, the call to " \
38
+ "devise_for must still exist) and call devise_scope :#{mapping.name} do ... end " \
39
+ "with the block instead", caller
40
+ yield
41
+ end
42
+
43
+ with_devise_exclusive_scope mapping.fullpath, mapping.name, options do
44
+ routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module PageFragments
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tasks/pf.rake ADDED
@@ -0,0 +1,5 @@
1
+ namespace :pf do
2
+ task :init => :environment do
3
+ require File.join(Rails.root, "config", "page_fragments")
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'page_fragments/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "page_fragments"
8
+ gem.version = PageFragments::VERSION
9
+ gem.authors = ["Alexandr Promakh"]
10
+ gem.email = ["s-promakh@ya.ru"]
11
+ gem.description = %q{ Storage page fragments in database }
12
+ gem.summary = gem.description
13
+ gem.homepage = "http://mystand.ru/"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.require_paths = ["lib"]
17
+
18
+ gem.required_ruby_version = ">= 1.9.2"
19
+
20
+ gem.license = "MIT"
21
+
22
+ gem.add_dependency "haml", "~> 4.0.3"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_fragments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexandr Promakh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: haml
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 4.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 4.0.3
30
+ description: ! ' Storage page fragments in database '
31
+ email:
32
+ - s-promakh@ya.ru
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - app/controllers/page_fragments/page_fragments_controller.rb
43
+ - app/helpers/page_fragments/page_fragment_helper.rb
44
+ - app/models/page_fragments/page_fragment.rb
45
+ - app/views/page_fragments/page_fragments/edit.html.haml
46
+ - app/views/page_fragments/page_fragments/index.html.haml
47
+ - config/routes.rb
48
+ - lib/generators/page_fragments/install_generator.rb
49
+ - lib/generators/page_fragments/templates/initialize.rb
50
+ - lib/generators/page_fragments/templates/migration.rb
51
+ - lib/generators/page_fragments/templates/page_fragments.en.yml
52
+ - lib/generators/page_fragments/templates/templates.rb
53
+ - lib/generators/page_fragments/views_generator.rb
54
+ - lib/page_fragments.rb
55
+ - lib/page_fragments/routes.rb
56
+ - lib/page_fragments/version.rb
57
+ - lib/tasks/pf.rake
58
+ - page_fragments.gemspec
59
+ homepage: http://mystand.ru/
60
+ licenses:
61
+ - MIT
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: 1.9.2
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.24
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Storage page fragments in database
84
+ test_files: []