sections 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392@foiv
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in section.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Daniil Zhirnov
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,24 @@
1
+ Формирование текстовых разделов с иерархией вложенности
2
+
3
+ ##Gemfile
4
+
5
+ gem 'sections'
6
+
7
+ или
8
+
9
+ gem 'sections', :path => "../gems/sections"
10
+ если гем во вложенной папке
11
+
12
+ затем в config/routes
13
+
14
+ sections_for NAME - создает пути и ресурс используемой модели (вводить имя модели)
15
+
16
+
17
+ ##Генерация модели и миграции
18
+
19
+ rails generate sections NAME - создает модель и миграцию, проверяет наличие модели (вводить имя модели)
20
+
21
+ rails g sections:install - копирование вьюх
22
+
23
+
24
+ Прогоните миграции rake db:migrate
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ class PagesController < ActionController::Base
2
+ # путь к вьюхам
3
+ prepend_view_path 'app/views/sections'
4
+
5
+ def index
6
+ resource_model = Sections.resource_model
7
+ @pages = resource_model.all
8
+ end
9
+
10
+ def new
11
+ @page = Sections.resource_model.new
12
+ end
13
+
14
+ def create
15
+ @page = Sections.resource_model.new params[Sections.resource_params_name]
16
+ @page.save
17
+ redirect_to sections_pages_path
18
+ end
19
+
20
+ def edit
21
+ @page = Sections.resource_model.find_by_id params[:id]
22
+ end
23
+
24
+ def update
25
+ @page = Sections.resource_model.find_by_id params[:id]
26
+ @page.update_attributes params[Sections.resource_params_name]
27
+ redirect_to sections_pages_path
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/sections/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class SectionsGenerator < ActiveRecord::Generators::Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ include Sections::Generators::OrmHelpers
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def copy_sections_migration
13
+ if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
14
+ migration_template "migration_existing.rb", "db/migrate/add_sections_to_#{table_name}"
15
+ else
16
+ migration_template "migration.rb", "db/migrate/sections_create_#{table_name}"
17
+ end
18
+ end
19
+
20
+ def generate_model
21
+ invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
22
+ end
23
+
24
+ def inject_sections_content
25
+ content = model_contents
26
+
27
+ class_path = if namespaced?
28
+ class_name.to_s.split("::")
29
+ else
30
+ [class_name]
31
+ end
32
+
33
+ indent_depth = class_path.size - 1
34
+ content = content.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n"
35
+
36
+ inject_into_class(model_path, class_path.last, content) if model_exists?
37
+ end
38
+
39
+ def migration_data
40
+ <<RUBY
41
+ t.string :name, :null => false, :default => ""
42
+ t.string :text
43
+ RUBY
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ class SectionsCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :<%= table_name %>, :name
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ class AddSectionsTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ change_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ # Uncomment below if timestamps were not included in your original model.
11
+ # t.timestamps
12
+ end
13
+
14
+ add_index :<%= table_name %>, :name
15
+ end
16
+
17
+ def self.down
18
+ # By default, we don't want to make any assumption about how to roll back a migration when your
19
+ # model already existed. Please edit below which fields you would like to remove in this migration.
20
+ raise ActiveRecord::IrreversibleMigration
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Sections
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ def copy_views
7
+ directory "views", "app/views/sections/pages/"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,44 @@
1
+ module Sections
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ buffer = <<-CONTENT if needs_attr_accessible?
6
+ # Setup accessible (or protected) attributes for your model
7
+ attr_accessible :name, :text
8
+
9
+ CONTENT
10
+ buffer
11
+ end
12
+
13
+ def needs_attr_accessible?
14
+ rails_3? && !strong_parameters_enabled?
15
+ end
16
+
17
+ def rails_3?
18
+ Rails::VERSION::MAJOR == 3
19
+ end
20
+
21
+ def strong_parameters_enabled?
22
+ defined?(ActionController::StrongParameters)
23
+ end
24
+
25
+ private
26
+
27
+ def model_exists?
28
+ File.exists?(File.join(destination_root, model_path))
29
+ end
30
+
31
+ def migration_exists?(table_name)
32
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_sections_to_#{table_name}.rb$/).first
33
+ end
34
+
35
+ def migration_path
36
+ @migration_path ||= File.join("db", "migrate")
37
+ end
38
+
39
+ def model_path
40
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ module Sections
2
+ module Generators
3
+ class SectionsGenerator < Rails::Generators::NamedBase
4
+ hook_for :orm
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ <%= f.text_field :name %>
2
+ <%= f.text_field :text %>
@@ -0,0 +1,3 @@
1
+ <div>
2
+ <%= page.name %>&nbsp;<%= page.text %>
3
+ </div>
@@ -0,0 +1,4 @@
1
+ <%= form_for @page, url: sections_page_path(@page) do |f| %>
2
+ <%= render partial: 'form', locals: {f: f} %>
3
+ <%= f.submit %>
4
+ <% end %>
@@ -0,0 +1,2 @@
1
+ <h1>Loaded!</h1>
2
+ <%= render partial: 'page', collection: @pages, as: 'page' %>
@@ -0,0 +1,4 @@
1
+ <%= form_for @page, url: sections_pages_path do |f| %>
2
+ <%= render partial: 'form', locals: {f: f} %>
3
+ <%= f.submit %>
4
+ <% end %>
data/lib/sections.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "sections/version"
2
+ require "sections/engine"
3
+
4
+ module Sections
5
+ def self.resource_model=(resource_model)
6
+ @@resource_model = resource_model.to_s.capitalize
7
+ end
8
+
9
+ def self.resource_model
10
+ @@resource_model.to_s.constantize
11
+ end
12
+
13
+ def self.resource_params_name
14
+ @@resource_model.downcase
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ require 'sections/rails/routes'
2
+
3
+ module Sections
4
+ class Engine < Rails::Engine
5
+
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+ def sections_for(resource_model)
4
+ namespace :sections do
5
+ resources :pages
6
+ end
7
+
8
+ Sections.resource_model = resource_model
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Sections
2
+ VERSION = "0.0.1"
3
+ end
data/sections.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sections/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sections"
8
+ spec.version = Sections::VERSION
9
+ spec.authors = ["Daniil Zhirnov"]
10
+ spec.email = ["danko.master@gmail.com"]
11
+ spec.description = %q{sections for site}
12
+ spec.summary = %q{Sample rails gem with generator}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sections
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniil Zhirnov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: sections for site
47
+ email:
48
+ - danko.master@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .ruby-version
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - app/controllers/pages_controller.rb
60
+ - lib/generators/active_record/sections_generator.rb
61
+ - lib/generators/active_record/templates/migration.rb
62
+ - lib/generators/active_record/templates/migration_existing.rb
63
+ - lib/generators/sections/install_generator.rb
64
+ - lib/generators/sections/orm_helpers.rb
65
+ - lib/generators/sections/sections_generator.rb
66
+ - lib/generators/templates/views/_form.html.erb
67
+ - lib/generators/templates/views/_page.html.erb
68
+ - lib/generators/templates/views/edit.html.erb
69
+ - lib/generators/templates/views/index.html.erb
70
+ - lib/generators/templates/views/new.html.erb
71
+ - lib/sections.rb
72
+ - lib/sections/engine.rb
73
+ - lib/sections/rails/routes.rb
74
+ - lib/sections/version.rb
75
+ - sections.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.25
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Sample rails gem with generator
101
+ test_files: []